#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cassert>
#include <numeric>


using namespace std;
#define ll long long

vector<int> prime(ll f){
  vector<int> ans(1,2);
  for(int i = 3;i <= sqrt(f);i ++){
    int p = 0;
    bool jud = true;
    while(ans[p]<=sqrt(i)){
      if(i % ans[p] == 0){
        jud = false;
        break;
      }
      p++;
    }
    if(jud == true){
      ans.insert(ans.end(),i);
    }
  }
  return ans;
}

int main(void){
  ll a,b;
  cin >> a >> b;
  while(a !=  b){
    if(a > b){
      if(a % b == 0){
        a = b;
      }else{
        a = a % b;
      }
    }else{
      if(b % a == 0){
        b = a;
      }else{
        b = b % a;
      }
    }
  }
  vector<int> pri = prime(a);
  vector<int> prnum(pri.size()+1,0);
  int s = pri.size();
  for(int i = 0;i < s;i ++){
    while(a % pri[i] == 0){
      a = a / pri[i];
      prnum[i] ++;
    }
    if(a == 1){
      break;
    }
  }
  if(a != 1){
    prnum[pri.size()]++;
  }
  int ans = 1;
  s = prnum.size();
  for(int i = 0;i < s;i ++){
    if(prnum[i]>0){
      ans++;
    }
  }
  cout << ans << endl;
  
  return 0;
}
#include <bits/stdc++.h>
using namespace std;

int main() {
  long n,ans=0,p=0;
  cin >> n;
  vector<vector<long>> a(n,vector<long>(n-1));
  vector<vector<bool>> b(n,vector<bool>(n));
  vector<long> c(n);
  queue<long> x;
  queue<long> y;
  for(long i=0;i<n;i++) for(long j=0;j<n-1;j++) cin >> a[i][j];

  for(long i=0;i<n;i++) {
    b[i][a[i][0]-1]=true;
    if(b[a[i][0]-1][i]) {
      x.push(i);
      y.push(a[i][0]-1);
    }
  }

  while(p<n*(n-1)/2) {
    ans++;
    long z=x.size();
    if(z==0) break;
    for(long i=0;i<z;i++) {
      c[x.front()]++;
      c[y.front()]++;
      if(c[x.front()]!=n-1) {
        b[x.front()][a[x.front()][c[x.front()]]-1]=true;
        if(b[a[x.front()][c[x.front()]]-1][x.front()]) {
          x.push(x.front());
          y.push(a[x.front()][c[x.front()]]-1);
        }
      }
      if(c[y.front()]!=n-1) {
        b[y.front()][a[y.front()][c[y.front()]]-1]=true;
        if(b[a[y.front()][c[y.front()]]-1][y.front()]) {
          x.push(y.front());
          y.push(a[y.front()][c[y.front()]]-1);
        }
      }
      x.pop();
      y.pop();
    }
    p+=z;
  }
  
  if(p==n*(n-1)/2) cout << ans << endl;
  else cout << -1 << endl;
}