#include <bits/stdc++.h>
using namespace std;
int g[200], vis[200];
int gcd(int a, int b) {
  int remainder = 0;
  do {
    remainder = a % b;
    a = b;
    b = remainder;
  } while (b != 0);
  return a;
}
int lcm(int a, int b) {
  if (a == 0 || b == 0) return 0;
  return (a * b) / gcd(a, b);
}
int main() {
  int counter, mxcnt, t, i, j;
  cin >> t;
  for (i = 1; i <= t; i++) cin >> g[i];
  for (i = 1, mxcnt = -2; i <= t; i++) {
    if (vis[i] == 0) {
      for (j = i, counter = 0; vis[j] != 1; counter++) {
        vis[j] = 1;
        j = g[j];
      }
      if (j != i) {
        mxcnt = -1;
        break;
      } else {
        if (counter % 2 == 0) counter = counter / 2;
        if (mxcnt == -2)
          mxcnt = counter;
        else {
          mxcnt = lcm(mxcnt, counter);
        }
      }
    }
  }
  cout << mxcnt;
  return 0;
}
