#include <bits/stdc++.h>
using namespace std;
struct __isoff {
  __isoff() {
    if (0) freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
    srand('C' + 'T' + 'A' + 'C' + 'Y' + 'M' + 'B' + 'A');
  }
  ~__isoff() {}
} __osafwf;
const unsigned long long p1 = 131;
const unsigned long long p2 = 129;
const double eps = 1e-8;
const double pi = acos(-1.0);
const int infi = 1e9 + 7;
const long long inf = 1e18 + 7;
const long long dd = 1e3 + 7;
const long long mod = 1e9 + 7;
int n;
vector<int> P;
long long gcd(long long a, long long b) {
  while (a && b) {
    a %= b;
    swap(a, b);
  }
  return a + b;
}
int main() {
  cin >> n;
  P.resize(n);
  for (int i = 0; i < (int)n; i++) {
    cin >> P[i];
    P[i]--;
  }
  long long c = 0;
  for (int i = 0; i < (int)n; i++) {
    int f = i;
    int l = 0;
    for (int j = 0; j < (int)n + 1; j++) {
      l++;
      f = P[f];
      if (f == i) {
        break;
      }
    }
    if (l == n + 1) {
      cout << "-1";
      return 0;
    }
    if (l % 2 == 0) {
      l /= 2;
    }
    if (c == 0) {
      c = l;
    } else {
      long long q = l / gcd(l, c);
      c *= q;
    }
  }
  cout << c;
}
