#include <bits/stdc++.h>
using namespace std;
int euclid(int m, int n) {
  int amari;
  if ((amari = m % n) == 0) {
    return n;
  } else {
    return euclid(n, amari);
  }
}
int main() {
  int N;
  int A[101];
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
  }
  vector<int> v;
  bool used[101] = {};
  bool flag = true;
  for (int i = 1; i <= N; i++) {
    if (!used[i]) {
      used[i] = true;
      int x = 1;
      int now = i;
      while (A[now] != i) {
        now = A[now];
        if (used[now]) {
          flag = false;
          break;
        }
        used[now] = true;
        x++;
      }
      v.push_back(x);
    }
  }
  if (!flag) {
    cout << -1 << endl;
    return 0;
  }
  int res = 1;
  for (int i = 0; i < v.size(); i++) {
    int z;
    if (v[i] % 2 == 0) {
      z = v[i] / 2;
    } else {
      z = v[i];
    }
    if (res > z) {
      res = res * z / euclid(res, z);
    } else {
      res = res * z / euclid(z, res);
    }
  }
  cout << res << endl;
  return 0;
}
