#include <bits/stdc++.h>
using namespace std;
int a[200];
int vis[200];
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
int main() {
  int n;
  scanf("%d", &n);
  long long minn = 1;
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) {
    memset(vis, 0, sizeof(vis));
    vis[i] = 1;
    long long cnt = 1;
    int now = i;
    while (1) {
      now = a[now];
      if (vis[now] == 1) {
        if (now != i) {
          printf("-1\n");
          return 0;
        }
        break;
      }
      vis[now] = 1;
      cnt++;
    }
    if (cnt % 2 == 0) cnt /= 2;
    minn = minn * cnt / (gcd(cnt, minn));
  }
  printf("%I64d\n", minn);
  return 0;
}
