#include <bits/stdc++.h>
using namespace std;
long long A[105];
long long cnt[105];
long long C[105];
long long gcd(long long a, long long b) {
  if (a > b) swap(a, b);
  while (a) {
    b %= a;
    swap(a, b);
  }
  return b;
}
int main() {
  long long n;
  scanf("%I64d", &n);
  for (long long i = 1; i <= n; i++) {
    scanf("%I64d", &A[i]);
  }
  for (long long i = 1; i <= n; i++) {
    bool done[105];
    memset(done, 0, sizeof(done));
    long long th = A[i];
    while (th != i) {
      if (done[th]) {
        puts("-1");
        return 0;
      }
      done[th] = 1;
      cnt[i]++;
      th = A[th];
    }
    cnt[i]++;
  }
  for (long long i = 1; i <= n; i++)
    C[i] = (cnt[i] % 2 == 0) ? cnt[i] / 2 : cnt[i];
  long long ans = 1;
  for (long long i = 1; i <= n; i++) ans = ans * C[i] / gcd(ans, C[i]);
  printf("%I64d\n", ans);
}
