#include <bits/stdc++.h>
using namespace std;
int pr[101];
int vi[101];
long long int tam[101];
void zeraVisited(int n) {
  for (int i = 1; i <= n; i++) {
    vi[i] = 0;
  }
}
long long int gcd(long long int a, long long int b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
int n = 0, b, ma = 0, mai = 0;
long long int a, c;
int cor[101];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &pr[i]);
  }
  for (int i = 1; i <= n; i++) {
    zeraVisited(n);
    int j = i;
    c = 0;
    while (!vi[j]) {
      vi[j] = 1;
      j = pr[j];
      c++;
    }
    if (j == i) {
      if (c & 1ll) {
        tam[i] = c;
      } else {
        tam[i] = c >> 1;
      }
    } else {
      printf("-1\n");
      return 0;
    }
  }
  a = tam[1];
  for (int i = 2; i <= n; i++) {
    a = (a * tam[i]) / gcd(a, tam[i]);
  }
  printf("%I64d\n", a);
  return 0;
}
