#include <bits/stdc++.h>
int gcd(int x, int y) {
  while (y) {
    int t = x % y;
    x = y;
    y = t;
  }
  return x;
}
int main() {
  int a[110], a1[110] = {0};
  int cut = 1;
  int ok = 1;
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    a1[a[i]]++;
    if (a1[a[i]] != 1) ok = 0;
  }
  if (ok == 0)
    printf("-1\n");
  else {
    for (int i = 1; i <= n; i++) {
      int b = i, nl = 1;
      while (a[b] != i) {
        b = a[b];
        nl++;
      }
      if (nl % 2 == 0) nl /= 2;
      cut = cut / gcd(cut, nl) * nl;
    }
    printf("%d\n", cut);
  }
  return 0;
}
