#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  while (b) {
    long long c = a % b;
    a = b;
    b = c;
  }
  return a;
}
int f[128];
bool vis[128];
int n;
int main() {
  scanf("%d", &n);
  for (int i = 0, to = n; i < to; ++i) scanf("%d", f + i), f[i]--;
  long long r = 1;
  for (int i = 0, to = n; i < to; ++i) {
    if (!vis[i]) {
      int j, l = 0;
      for (j = i; !vis[j]; j = f[j]) vis[j] = true, l++;
      if (j != i) {
        cout << "-1\n";
        return 0;
      }
      if (l % 2 == 0) l /= 2;
      r = (r * l) / gcd(r, l);
    }
  }
  cout << r << endl;
  return 0;
}
