#include <bits/stdc++.h>
using namespace std;
inline void deBugMode() {}
inline void optimizeIt() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
}
inline int MOD(const int &x) { return max(x, -x); }
const int NMax = 105;
const int LIM = 1e9;
int v[NMax];
bool viz[NMax];
inline int gcd(int a, int b) {
  int r;
  while (b != 0) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
int main() {
  deBugMode();
  optimizeIt();
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> v[i];
  int sol = 1;
  int no = 0;
  for (int i = 1; i <= n; i++) {
    if (viz[i] == false) {
      no++;
      int len = 0;
      int now = i;
      while (viz[now] == false) {
        viz[now] = true;
        len++;
        now = v[now];
      }
      if (now != i) {
        cout << -1;
        return 0;
      }
      if (len % 2 == 0) len /= 2;
      int cmmdc = gcd(sol, len);
      sol = (sol * len) / cmmdc;
    }
  }
  cout << sol;
  return 0;
}
