#include <bits/stdc++.h>
using namespace std;
int n, a[200], col = 1;
bool b[200];
int gcd(int a, int b) {
  for (;;) {
    if (a == 0) return b;
    b %= a;
    if (b == 0) return a;
    a %= b;
  }
}
int lcm(int a, int b) {
  int temp = gcd(a, b);
  return temp ? (a / temp * b) : 0;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 1; i <= n; i++) {
    int ci = i;
    int len = 0;
    if (b[ci]) continue;
    do {
      len++;
      b[ci] = true;
      ci = a[ci];
    } while (!b[ci]);
    if (i != ci) {
      printf("-1");
      return 0;
    }
    if (len % 2 == 0) len /= 2;
    col = lcm(col, len);
  }
  printf("%d", col);
  return 0;
}
