#include <bits/stdc++.h>
using namespace std;
long long c[105];
long long cyclen[105];
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
int main() {
  long long N;
  cin >> N;
  for (long long i = 0; i < N; i++) {
    cin >> c[i];
    c[i]--;
  }
  for (long long x = 0; x < N; x++) {
    long long y = c[x];
    bool seen[105];
    memset(seen, 0, sizeof seen);
    cyclen[x] = 1;
    while (y != x) {
      cyclen[x]++;
      if (seen[y] == 1) {
        cout << -1 << endl;
        return 0;
        break;
      }
      seen[y] = 1;
      y = c[y];
    }
    if (cyclen[x] % 2 == 0) cyclen[x] /= 2;
  }
  long long ans = cyclen[0];
  for (long long i = 0; i < N; i++) {
    ans = lcm(ans, cyclen[i]);
  }
  cout << ans << endl;
}
