#include <bits/stdc++.h>
using namespace std;
const int N = 105;
int a[N];
bool visit[N];
inline long long gcd(long long a, long long b) {
  long long c;
  while (b) {
    c = b;
    b = a % b;
    a = c;
  }
  return a;
}
long long lcm(long long a, long long b) {
  long long c = gcd(a, b);
  return a / c * b;
}
int main() {
  {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  };
  int n, i, j, len;
  set<int> S;
  cin >> n;
  bool ok = true;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    if (S.count(a[i]) > 0) {
      ok = false;
    }
    S.insert(a[i]);
  }
  if (!ok) {
    cout << -1 << endl;
    return 0;
  }
  S.clear();
  for (i = 1; i <= n; i++) {
    if (!visit[i]) {
      len = 1;
      visit[i] = true;
      for (j = a[i]; j != i; j = a[j], len++) {
        visit[j] = true;
      }
      if (len % 2 == 0) {
        len /= 2;
      }
      S.insert(len);
    }
  }
  long long ans = 1;
  for (auto l : S) {
    ans = lcm(ans, l);
  }
  cout << ans << endl;
  return 0;
}
