#include <bits/stdc++.h>
using namespace std;
int x[110], c[110], v[110];
long long gcd(long long a, long long b) {
  if (!b) return a;
  return gcd(b, a % b);
}
inline long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
int main() {
  int n;
  cin >> n;
  for (int i = (1); i < (n + 1); i++) cin >> x[i];
  for (int i = (1); i < (n + 1); i++) {
    int j = i, k = 0;
    while (!v[j]) {
      v[j] = 1;
      k++;
      j = x[j];
    }
    if (j != i) {
      cout << "-1\n";
      return 0;
    }
    c[i] = k;
    memset(v, 0, sizeof(v));
  }
  long long l = 1;
  for (int i = (1); i < (n + 1); i++) l = lcm(l, c[i]);
  while (l % 2 == 0) {
    int f = 1;
    long long k = l / 2;
    for (int i = (1); i < (n + 1); i++)
      f &= (k % c[i] == 0 || (c[i] % 2 == 0 && k % (c[i] / 2) == 0));
    if (!f) break;
    l = k;
  }
  cout << l << "\n";
  return 0;
}
