#include <bits/stdc++.h>
using namespace std;
int a[102];
bool b[102];
int gcd(int x, int y) {
  if (x % y == 0)
    return y;
  else
    return gcd(y, x % y);
}
int main() {
  int n;
  int ans = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    b[i] = true;
  }
  int j = 1, k = 1;
  int r;
  while (j <= n) {
    k = j;
    r = 1;
    while (b[k]) {
      b[k] = false;
      k = a[k];
      if (k != j)
        r++;
      else {
        if (r % 2 == 0) r /= 2;
        if (ans == 0) {
          ans = r;
        } else if (ans != -1)
          ans = (ans * r) / gcd(ans, r);
      }
    }
    if (!b[k] && (k != j)) {
      ans = -1;
      break;
    }
    j++;
  }
  if (ans == 0) ans = -1;
  printf("%d\n", ans);
  return 0;
}
