#include <bits/stdc++.h>
using namespace std;
long long cnt = 0;
bool ck[100005];
int vec[100005];
long long _gcd(long long x, long long y) {
  if (x == 0)
    return y;
  else
    return _gcd(y % x, x);
}
void dfs(int ind, int vv) {
  ck[ind] = true;
  cnt++;
  if (vec[ind] == vv) return;
  if (!ck[vec[ind]]) {
    dfs(vec[ind], vv);
  } else
    cnt = -1;
}
int main() {
  long n;
  scanf("%ld", &n);
  long long ans = 1;
  for (int i = 1; i <= n; i++) {
    long xx;
    scanf("%ld", &xx);
    vec[i] = xx;
  }
  for (int i = 1; i <= n; i++) {
    if (!ck[i]) {
      cnt = 0;
      dfs(i, i);
      if (cnt == -1) {
        cout << "-1";
        return 0;
      }
      if (cnt % 2 == 0) cnt /= 2;
      long long vv = ans;
      ans *= cnt;
      ans /= _gcd(vv, cnt);
    }
  }
  cout << ans;
  return 0;
}
