Problem: problem

A dance party with $ N $ people is held at a dance hall. The dance hall is divided into $ H $ vertical and $ W $ horizontal grids, with $ (0,0) $ in the upper left, $ r $ squares from the top, and $ c $ squares from the left. The coordinates of the grid of the eyes are expressed as $ (r, c) $. The initial position of the $ i $ th participant is $ (R_i, C_i) $, and the $ (i, j) $ grid contains $ (r_ {ij}, c_ {ij}) $. ..

Each participant simultaneously moves to the endless music as follows.

* When the coordinates at that time are $ (i, j) $, jump to $ (r_ {ij}, c_ {ij}) $.



Each grid is narrow and will collide if two or more participants move to the same grid at the same time. However, it is assumed that there is no collision in the air. When you heard this, you were worried that two or more participants would collide after the jump. Therefore, I decided to find out if there was a possibility of a collision, and if so, how many jumps the collision would occur after.



output

If a collision occurs, output in one line how many jumps it will occur after. If not, output -1. Also, output a line break at the end.

Example

Input

2 2 2
1 0 0 1
0 0 1 0
0 0
0 1


Output

-1

Solution (in CPP):

Solution:
#include<bits/stdc++.h>
using namespace std;

#define int long long

#define rep(i,n) for(int i=0;i<(n);i++)
#define all(v) (v).begin(),(v).end()
#define pb push_back
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;
#define fi first
#define se second
template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

int H,W,N;
int M;
int to[500*500];

int id[500*500];

vector<vint>loop;

void findLoop(){
    vector<int>deg(H*W);
    queue<int>que;
    fill_n(id,500*500,-2);
    rep(i,M)deg[to[i]]++;
    rep(i,M)if(deg[i]==0)que.push(i),id[i]=-1;
    while(que.size()){
        int v=que.front();
        que.pop();
        int u=to[v];
        if(--deg[u]==0){
            id[u]=-1;
            que.push(u);
        }
    }

    rep(i,M){
        if(id[i]!=-2)continue;
        loop.pb(vint());
        int v=i;
        while(id[v]==-2){
            id[v]=loop.size()-1;
            loop.back().pb(v);
            v=to[v];
        }
    }
}

vint T[500*500];
int ans;

int dep[500*500];
set<int>ds[500*500];
bool ex[500*500];
void dfs(int v,int d){
    dep[v]=d;
    if(ex[v])ds[v].insert(d);
    for(auto u:T[v]){
        dfs(u,d+1);
        if(ds[v].size()<ds[u].size())swap(ds[v],ds[u]);
        for(auto it=ds[u].begin();it!=ds[u].end();it++){
            if(ds[v].find(*it)!=ds[v].end()){
                chmin(ans,*it-d);
            }
            ds[v].insert(*it);
        }
    }
}

bool C(int t){
    for(auto &vs:loop){
        vint cnt(vs.size());
        rep(i,vs.size()){
            for(auto u:ds[vs[i]]){
                if(u>t)continue;
                cnt[(i+t-u)%vs.size()]++;
            }
        }
        rep(i,vs.size())if(cnt[i]>1)return true;
    }
    return false;
}

signed main(){
    scanf("%lld%lld%lld",&H,&W,&N);
    M=H*W;
    rep(i,H){
        rep(j,W){
            int a,b;
            scanf("%lld%lld",&a,&b);
            to[i*W+j]=a*W+b;
        }
    }
    vint vs;
    rep(i,N){
        int a,b;
        scanf("%lld%lld",&a,&b);
        vs.pb(a*W+b);
        ex[a*W+b]=1;
    }

    findLoop();

    rep(i,M){
        if(id[i]!=-1)continue;
        T[to[i]].pb(i);
    }

    ans=INT_MAX;

    rep(i,M){
        if(id[i]==-1)continue;
        dfs(i,0);
    }

    int lb=0,ub=INT_MAX;
    while(ub-lb>1){
        int mid=(ub+lb)/2;
        if(C(mid))ub=mid;
        else lb=mid;
    }

    chmin(ans,ub);
    if(ans==INT_MAX)cout<<-1<<endl;
    else cout<<ans<<endl;
    return 0;
}