#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100;
int n, crush[MAX_N + 1];
long long ans;
bool isUsed[MAX_N + 1];
long long gcd(long long a, long long b) {
  while (a != 0 && b != 0) {
    a %= b;
    swap(a, b);
  }
  return a + b;
}
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie();
  cout.tie();
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> crush[i];
  }
  ans = 1;
  for (int i = 1; i <= n; i++) {
    isUsed[i] = false;
  }
  for (int i = 1; i <= n; i++) {
    int v = crush[i], d = 1;
    if (!isUsed[i]) {
      isUsed[i] = true;
      while (v != i) {
        if (isUsed[v]) {
          cout << -1 << endl;
          return 0;
        }
        isUsed[v] = true;
        v = crush[v];
        d++;
      }
    }
    if (d % 2 == 0) {
      d /= 2;
    }
    ans = lcm(ans, d);
  }
  cout << ans << endl;
  return 0;
}
