#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  if (a < b) swap(a, b);
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
int lcm(int a, int b) { return a * b / gcd(a, b); }
int main() {
  int a[105][105] = {0};
  int ans = 1;
  int cnt = 0;
  int b[105] = {0};
  int c[105] = {0};
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    b[i] = x;
    a[i][x] = 1;
    c[i] = 0;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (a[j][i] == 0) cnt++;
    }
    if (cnt == n) {
      break;
    }
    cnt = 0;
  }
  if (cnt == n)
    cout << "-1" << endl;
  else {
    while (true) {
      int i = 1;
      for (; i <= n; i++) {
        if (c[i] == 0) break;
      }
      if (i == n + 1) break;
      int x = b[i];
      cnt = 1;
      c[i] = 1;
      while (x != i) {
        c[x] = 1;
        x = b[x];
        cnt++;
      }
      if (cnt % 2 == 0) cnt /= 2;
      ans = lcm(ans, cnt);
    }
    cout << ans << endl;
  }
  return 0;
}
