#include <bits/stdc++.h>
using namespace std;
vector<long long> v;
bitset<101> usado;
long long dfs(long long nodo) {
  if (usado[nodo]) return 0;
  usado.set(nodo);
  return 1 + dfs(v[nodo]);
}
long long calcmcd(long long a, long long b) {
  if (a % b == 0) return b;
  return calcmcd(b, a % b);
}
int main() {
  ios::sync_with_stdio(false);
  bool puede = true;
  long long n, aux, resp, mcd, mcm;
  vector<long long> pararesp;
  cin >> n;
  v.resize(n + 1);
  for (long long i = 1; i <= n; i++) {
    cin >> v[i];
    if (usado[v[i]])
      puede = false;
    else
      usado.set(v[i]);
  }
  if (puede) {
    usado.reset();
    for (long long i = 1; i <= n; i++) {
      while (i <= n && (usado[i] || v[i] == i)) i++;
      if (i <= n) {
        aux = dfs(i);
        if (aux % 2 == 0) aux /= 2;
        pararesp.push_back(aux);
      }
    }
    if (pararesp.empty())
      resp = 1;
    else {
      if (pararesp.size() == 1)
        resp = pararesp[0];
      else {
        mcm = pararesp[pararesp.size() - 1];
        for (int i = pararesp.size() - 2; i >= 0; i--) {
          if (pararesp[i] > mcm)
            mcd = calcmcd(pararesp[i], mcm);
          else
            mcd = calcmcd(mcm, pararesp[i]);
          mcm = mcm * pararesp[i] / mcd;
        }
        resp = mcm;
        pararesp.clear();
      }
    }
    cout << resp << "\n";
  } else
    cout << "-1\n";
  v.clear();
  return 0;
}
