#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return (a ? gcd(b % a, a) : b); }
int main() {
  int n, arr[112], in[112] = {0};
  while (cin >> n) {
    long long ans = -1;
    for (int i = 1; i <= n; i++) cin >> arr[i], in[arr[i]]++;
    for (int i = 1; i <= n; i++) {
      int aye = arr[i], t = 1;
      while (aye != i && t <= n) {
        t++;
        aye = arr[aye];
      }
      if (t == n + 1) {
        cout << -1 << endl;
        return 0;
      }
      if (!(t % 2)) t /= 2;
      ans = (ans * t) / gcd(ans, t);
    }
    cout << ans << endl;
  }
}
