#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)(1e2 + 11);
const int inf = (int)(1e9 + 7);
const int base = (int)(1e9 + 7);
const int mod = (int)(1e9 + 7);
int crush[maxn], ans[maxn], res;
bool used[maxn];
int gcd(int a, int b) {
  if (!b) return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
int main() {
  ios_base ::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> crush[i];
  }
  for (int t = 1; t <= n; ++t) {
    for (int i = 1; i <= n; ++i) {
      if (used[i]) continue;
      int cur = i, time = t, j = i;
      while (1) {
        cur = crush[cur];
        j = cur;
        if (time == 1) break;
        if (cur == crush[cur]) break;
        time--;
      }
      if (j == i) {
        used[i] = 1;
        ans[i] = t;
        continue;
      }
      cur = j, time = t;
      int y = j;
      while (1) {
        cur = crush[cur];
        y = cur;
        if (time == 1) break;
        if (cur == crush[cur]) break;
        time--;
      }
      if (y == i) {
        used[i] = 1;
        ans[i] = t;
      }
    }
  }
  res = 1;
  for (int i = 1; i <= n; ++i) {
    if (!ans[i]) {
      cout << -1;
      return 0;
    }
    res = lcm(res, ans[i]);
  }
  cout << res;
  return 0;
}
