#include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
int a[maxn], mark[maxn];
long long gcd(long long a, long long b) {
  if (b > a) swap(a, b);
  long long c;
  while (b > 0) {
    c = a % b;
    a = b;
    b = c;
  }
  return a;
}
int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    memset(mark, 0, sizeof(mark));
    for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
    long long ans = 1;
    int flag = 0;
    for (int i = 1; i <= n; ++i) {
      if (mark[i]) continue;
      long long temp = 1;
      int y = a[i];
      mark[i] = 1;
      while (y != i) {
        mark[y] = 1;
        y = a[y];
        temp++;
        if (temp > n) {
          flag = 1;
          break;
        }
      }
      if (flag) break;
      if (temp % 2 == 0) temp /= 2;
      ans = ans / gcd(ans, temp) * temp;
    }
    if (flag)
      printf("-1\n");
    else
      printf("%lld\n", ans);
  }
  return 0;
}
