#include <bits/stdc++.h>
long long int gcd(int a, long long int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
void lcm(int arr[], int n) {
  long long int ans = arr[1];
  for (int i = 2; i <= n; i++) ans = (((arr[i] * ans)) / (gcd(arr[i], ans)));
  printf("%lli", ans);
}
int main() {
  int n, i, arr[100];
  int steps[100];
  scanf("%d", &n);
  for (i = 1; i <= n; i++) scanf("%d", &arr[i]);
  for (i = 1; i <= n; i++) {
    int count = 0;
    int j = i;
    while (arr[j] != i) {
      if (count == n) {
        printf("-1");
        return 0;
      }
      j = arr[j];
      count++;
    }
    count++;
    if (count % 2 == 0) {
      steps[i] = count / 2;
    } else {
      steps[i] = count;
    }
  }
  lcm(steps, n);
  return 0;
}
