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;

typedef long long ll;
typedef pair<ll, ll> P;

#define EACH(i,a) for (auto& i : a)
#define FOR(i,a,b) for (ll i = (a); i < (b); ++i)
#define RFOR(i,a,b) for (ll i=(b)-1; i>=a;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define pb push_back
#define ALL(a) (a).begin(),(a).end()

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template<typename T>
istream& operator>>(istream& is, vector<T>& vec) {
    EACH(x,vec) is >> x;
    return is;
}
template<typename T>
ostream& operator<<(ostream& os, vector<T>& vec) {
    REP(i,vec.size()) {
        if (i) os << " ";
        os << vec[i];
    }
    return os;
}
template<typename T>
ostream& operator<<(ostream& os, vector<vector<T> >& vec) {
    REP(i,vec.size()) {
        if(i) os << endl;
        os << vec[i];
    }
    return os;
}

struct Point {
    int x, y;
};

typedef vector<vector<P>> Matrix;

int W, H;
Matrix A;
vector<P> Human;

Matrix dot(const Matrix& a, const Matrix& b) {
    Matrix res(H, vector<P>(W, {-1, -1}));
    REP(y, H) REP(x, W) {
        P p = a[y][x];
        res[y][x] = b[p.second][p.first];
    }
    return res;
}
vector<P> dot(const vector<P>& human, const Matrix& conv) {
    vector<P> res;
    EACH(h, human) {
        res.pb(conv[h.second][h.first]);
    }
    return res;
}
Matrix E(int w, int h) {
    Matrix res(h, vector<P>(w, {-1, -1}));
    REP(y, h) REP(x, w) {
        res[y][x] = {x, y};
    }
    return res;
}
Matrix power(Matrix X, int n) {
    Matrix res = E(W, H);
    for (ll i = 1; i <= n; i <<= 1) {
        if (i & n) res = dot(res, X);
        X = dot(X, X);
    }
    return res;
}

bool is_conflict(int step) {
    vector<P> after = dot(Human, power(A, step));
    set<P> s;
    EACH(p, after) s.insert(p);
    return s.size() != after.size();
}
bool is_dup(vector<P> after) {
    int ps = after.size();
    sort(ALL(after));
    after.erase(unique(ALL(after)), after.end());
    return after.size() != ps;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n; cin >> H >> W >> n;
    A.assign(H, vector<P>(W, {-1, -1}));
    REP(y, H) REP(x, W) {
        int ny, nx; cin >> ny >> nx;
        A[y][x] = {nx, ny};
    }
    REP(i, n) {
        int y, x; cin >> y >> x;
        Human.pb({x, y});
    }
    ll l = 0, r = W*H;
    if (!is_conflict(r)) {
        cout << -1 << endl;
    }
    else {
        int lg = 0;
        for (ll i = 1; i <= W*H; i <<= 1, ++lg);
        vector<Matrix> powX;
        powX.pb(A);
        REP(i, lg) {
            powX.pb(dot(powX[i], powX[i]));
        }
        Matrix X = E(W, H);
        ll ans = 0;
        RREP(i, lg) {
            Matrix nX = dot(X, powX[i]);
            if ( !is_dup(dot(Human, nX)) ) {
                ans += (1<<i);
                X = nX;
            }
        }
        ++ans;
        cout << ans << endl;
        // REP(t, 30) {
        //     int m = (l + r) / 2;
        //     if (is_conflict(m)) {
        //         r = m;
        //     }
        //     else {
        //         l = m;
        //     }
        // }
        // cout << r << endl;
    }
}