N players will participate in a tennis tournament. We will call them Player 1, Player 2, \ldots, Player N.

The tournament is round-robin format, and there will be N(N-1)/2 matches in total. Is it possible to schedule these matches so that all of the following conditions are satisfied? If the answer is yes, also find the minimum number of days required.

* Each player plays at most one matches in a day.
* Each player i (1 \leq i \leq N) plays one match against Player A_{i, 1}, A_{i, 2}, \ldots, A_{i, N-1} in this order.

Constraints

* 3 \leq N \leq 1000
* 1 \leq A_{i, j} \leq N
* A_{i, j} \neq i
* A_{i, 1}, A_{i, 2}, \ldots, A_{i, N-1} are all different.

Input

Input is given from Standard Input in the following format:


N
A_{1, 1} A_{1, 2} \ldots A_{1, N-1}
A_{2, 1} A_{2, 2} \ldots A_{2, N-1}
:
A_{N, 1} A_{N, 2} \ldots A_{N, N-1}


Output

If it is possible to schedule all the matches so that all of the conditions are satisfied, print the minimum number of days required; if it is impossible, print `-1`.

Examples

Input

3
2 3
1 3
1 2


Output

3


Input

4
2 3 4
1 3 4
4 1 2
3 1 2


Output

4


Input

3
2 3
3 1
1 2


Output

-1
import java.io.*;
import java.util.*;

public class Main {

    private static FastReader in = new FastReader(System.in);
    private static PrintWriter out = new PrintWriter(System.out);

    private static final long MOD = (long) 1e9 + 7;

    public static void main(String[] args) {
        solve();
        out.flush();
    }

    private static void solve() {
        int n = in.nextInt();
        List<Queue<Integer>> a = new ArrayList<>();
        for (int i=0; i < n; i++) {
            a.add(new ArrayDeque<>());
            for (int j=0; j < n - 1; j++) {
                a.get(i).add(in.nextInt() - 1);
            }
        }

        // https://atcoder.jp/contests/abc139/submissions/7293795
        int days = 0;
        int[] latest = new int[n];
        Queue<Integer> toCheck = new ArrayDeque<>();
        for (int i=0; i < n; i++)
            toCheck.add(i);
        int remaining = n * (n-1) / 2;
        while (!toCheck.isEmpty()) {
            int me = toCheck.poll();
            if (a.get(me).isEmpty()) continue;
            int opp = a.get(me).peek();
            if (a.get(opp).isEmpty()) continue;
            if (a.get(opp).peek() == me) {
                remaining--;
                a.get(me).poll();
                a.get(opp).poll();
                toCheck.add(me);
                toCheck.add(opp);
                latest[me] = latest[opp] = Math.max(latest[me], latest[opp]) + 1;
                days = Math.max(latest[me], days);
            }
        }

        out.println(remaining == 0 ? days : -1);
    }

    private static boolean check(Queue<Integer>[] a, int n) {
        for (Queue<Integer> d : a) {
            if (!d.isEmpty())
                return false;
        }
        return true;
    }

    // https://www.geeksforgeeks.org/fast-io-in-java-in-competitive-programming/
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader(InputStream inputStream) {
            br = new BufferedReader(new
                    InputStreamReader(inputStream));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException  e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        // https://qiita.com/masakinpo/items/5f77f2a879578a85a0bb
        public int[] nextIntArray(int n) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++)
                a[i] = nextInt();
            return a;
        }

        public long[] nextLongArray(int n) {
            long[] a = new long[n];
            for (int i = 0; i < n; i++)
                a[i] = nextLong();
            return a;
        }
    }

}
