#include <bits/stdc++.h>
const int MAX_N = 128;
int link[MAX_N];
char used[MAX_N];
int get_len(int bg) {
  int d = 0;
  int v = bg;
  while (true) {
    if (v == bg && used[v]) return d;
    if (used[v]) return -1;
    used[v] = true;
    v = link[v];
    ++d;
  }
}
int gcd(int a, int b) { return a ? gcd(b % a, a) : b; }
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) scanf("%d", link + i), link[i]--;
  int ans = 1;
  for (int i = 0; i < n; ++i)
    if (!used[i]) {
      int d = get_len(i);
      if (d == -1) {
        printf("-1\n");
        return 0;
      }
      if (d % 2 == 0) d /= 2;
      ans = ans * d / gcd(d, ans);
    }
  printf("%d\n", ans);
  return 0;
}
