#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  while (b != 0) {
    long long c = a % b;
    a = b;
    b = c;
  }
  return a;
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
int main() {
  int n, a[105], i;
  long long ans = 1, cnt;
  bool visit[105];
  bool indeg[105];
  memset(indeg, false, sizeof(indeg));
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    indeg[a[i]] = true;
  }
  for (i = 1; i <= n; i++) {
    if (!indeg[i]) {
      cout << "-1" << endl;
      return 0;
    }
  }
  memset(visit, false, sizeof(visit));
  for (i = 1; i <= n; i++) {
    if (!visit[i]) {
      cnt = 0;
      int cur = i;
      while (!visit[cur]) {
        cnt++;
        visit[cur] = true;
        cur = a[cur];
      }
    }
    if (cnt % 2 == 0) cnt /= 2;
    ans = lcm(ans, (long long)cnt);
  }
  cout << ans << endl;
}
