#include <bits/stdc++.h>
using namespace std;
template <class X, class Y>
void amax(X& x, const Y& y) {
  if (x < y) x = y;
}
template <class X, class Y>
void amin(X& x, const Y& y) {
  if (x > y) x = y;
}
const int INF = 1e9 + 10;
const long long INFL = 1e18 + 10;
const int MAX = 110;
int n;
int nxt[MAX], value[MAX];
int f[MAX][MAX];
long long gcd(long long a, long long b) {
  long long r = 0;
  while (b != 0) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
void process() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> nxt[i];
  for (int T = 1; T <= n; T++) {
    for (int i = 1; i <= n; i++) {
      int y = i;
      for (int j = 1; j <= T; j++) {
        y = nxt[y];
        f[i][j] = y;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int T = 1; T <= n; T++) {
      if (i == f[f[i][T]][T]) {
        value[i] = T;
        break;
      }
    }
    if (value[i] == 0) {
      cout << -1;
      return;
    }
  }
  long long res = 1;
  for (int i = 1; i <= n; i++) res = lcm(res, value[i]);
  cout << res;
}
int main() {
  ios_base::sync_with_stdio(false);
  process();
}
