#include <bits/stdc++.h>
using namespace std;
int n, crush[105], cnt, pre;
int k[105];
long long ans = 0;
long long arrLCM(int* arr, int sz);
long long gcd(long long x, int y);
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) scanf("%d", crush + i);
  memset(k, 0, n);
  for (int i = 0; i < n; ++i) {
    int j = crush[i] - 1;
    pre = -1;
    for (cnt = 1; cnt < n + 1; ++cnt) {
      if (j == pre) break;
      if (j == i) {
        k[i] = (cnt & 1 ? cnt : cnt / 2);
        break;
      }
      pre = j;
      j = crush[j] - 1;
    }
    if (k[i] == 0) {
      ans = -1;
      break;
    }
  }
  if (ans == 0) ans = arrLCM(k, n);
  printf("%lld\n", ans);
}
long long gcd(long long x, int y) {
  if (x == 0) return y;
  if (y == 0) return x;
  x = x % y;
  return gcd(y, x);
}
long long arrLCM(int* arr, int sz) {
  long long lcm = arr[0];
  for (int i = 1; i < sz; ++i) {
    lcm = (lcm * arr[i]) / gcd(lcm, arr[i]);
  }
  return lcm;
}
