#include <bits/stdc++.h>
using namespace std;
const long long inf = 4e18;
const long long maxn = 110;
const long long mod = 1e9 + 7;
long long dg[maxn], f[maxn];
bool vis[maxn];
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int main() {
  long long n;
  cin >> n;
  for (int i = int(1); i <= int(n); i++) {
    cin >> f[i];
    dg[f[i]]++;
  }
  for (int i = int(1); i <= int(n); i++) {
    if (dg[i] != 1) {
      cout << -1;
      return 0;
    }
  }
  long long ans = 1;
  for (int i = int(1); i <= int(n); i++) {
    if (!vis[i]) {
      long long tmp = i, cnt = 0;
      while (!vis[tmp]) {
        vis[tmp] = 1;
        cnt++;
        tmp = f[tmp];
      }
      ans = (ans * cnt) / gcd(ans, cnt);
    }
  }
  if (ans % 2 == 0) ans /= 2;
  cout << ans;
}
