#include <bits/stdc++.h>
using namespace std;
const int maxn = 101;
const int INF = 0x3f3f3f3f;
int flag[maxn];
int a[maxn];
int n, k;
int Ans = 0;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
int main() {
  scanf("%d", &n);
  Ans = 1;
  ;
  memset(flag, 0, sizeof(flag));
  ;
  int x;
  int count = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &x);
    if (flag[x]) {
      count = -1;
    }
    a[i] = x;
    flag[x] = 1;
  }
  if (count == -1) {
    printf("-1");
  } else {
    ;
    memset(flag, 0, sizeof(flag));
    ;
    int res = 1, fla;
    for (int i = 1; i <= n; i++) {
      if (flag[i]) continue;
      int count = 0;
      fla = i;
      while (!flag[fla]) {
        count++;
        flag[fla] = 1;
        fla = a[fla];
      }
      if (count) {
        if (count % 2 == 0) count /= 2;
        Ans = lcm(count, Ans);
      }
    }
    printf("%d\n", Ans);
  }
  return 0;
}
