#include <bits/stdc++.h>
using namespace std;
const int NMAX = 1000;
long long f[1000006];
long long Ans = 1;
vector<long long> lant;
long long v[NMAX + 2], viz[NMAX + 2];
long long GCD(long long a, long long b) {
  while (b) {
    long long r = a % b;
    a = b;
    b = r;
  }
  return a;
}
long long LCM(long long a, long long b) { return a / GCD(a, b) * b; }
int main() {
  long long N;
  cin >> N;
  for (long long i = 1; i <= N; ++i) {
    cin >> v[i];
  }
  for (long long i = 1; i <= N; ++i) {
    if (!viz[i]) {
      long long p = i;
      long long lg = 0;
      while (!viz[p]) {
        viz[p] = 1;
        ++lg;
        p = v[p];
      }
      lant.push_back(lg);
    }
  }
  sort(v + 1, v + N + 1);
  bool ok = 1;
  for (long long i = 1; i <= N; ++i)
    if (v[i] != i) ok = 0;
  if (!ok) {
    cout << "-1\n";
    return 0;
  }
  for (auto x : lant) {
    long long heh = x;
    if (x % 2 == 0) heh /= 2;
    Ans = LCM(Ans, heh);
  }
  cout << Ans << '\n';
  return 0;
}
