#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
int main() {
  int n;
  cin >> n;
  vector<int> a(n), loop(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  for (int i = 0; i < n; i++) {
    int temp = 1, j = i;
    while (a[j] != i) {
      j = a[j];
      temp++;
      if (temp > n + 1) {
        cout << -1;
        return 0;
      }
    }
    if (temp % 2 == 0) {
      temp = temp / 2;
    }
    loop[i] = temp;
  }
  long long int lcm = 1;
  for (int i = 0; i < n; i++) {
    int gd = gcd(lcm, loop[i]);
    lcm = (lcm * loop[i]) / gd;
  }
  cout << lcm << endl;
  return 0;
}
