#include <bits/stdc++.h>
using namespace std;
class C_ {};
template <typename T>
C_& operator<<(C_& __m, const T& __s) {
  if (!1) cerr << "\E[91m" << __s << "\E[0m";
  return __m;
}
C_ merr;
struct __s {
  __s() {
    if (1) {
      ios_base::Init i;
      cin.sync_with_stdio(0);
      cin.tie(0);
    }
  }
  ~__s() {
    merr << "Execution time: " << (double)clock() / CLOCKS_PER_SEC << " s.\n";
  }
} __S;
int P[111];
int dp[111][28];
int go(int v, int cnt) {
  for (size_t i = 0; i < 28; i++) {
    if (cnt & (1 << i)) {
      v = dp[v][i];
    }
  }
  return v;
}
long long gcd(long long a, long long b) { return a ? gcd(b % a, a) : b; }
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
int main(void) {
  size_t n;
  cin >> n;
  for (size_t i = 0; i < n; i++) {
    cin >> P[i];
    P[i]--;
    dp[i][0] = P[i];
  }
  for (size_t j = 1; j < 28; j++) {
    for (size_t i = 0; i < n; i++) {
      dp[i][j] = dp[dp[i][j - 1]][j - 1];
    }
  }
  bool uu[1111] = {};
  long long ans = 1;
  for (size_t i = 0; i < n; i++) {
    if (uu[i]) continue;
    merr << i << '\n';
    int st = dp[i][27];
    uu[i] = 1;
    uu[st] = 1;
    int x = P[st];
    int cnt = 1;
    while (x != st) {
      x = P[x];
      uu[x] = 1;
      cnt++;
    }
    if (go((int)i, cnt) != (int)i) {
      cout << "-1" << '\n';
      return 0;
    }
    if (cnt % 2 == 0) {
      ans = lcm(ans, cnt / 2);
    } else {
      ans = lcm(ans, cnt);
    }
  }
  cout << ans << '\n';
  return 0;
}
