#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int a[105], b[105], book[105];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  if (n == 1) {
    printf("1\n");
    return 0;
  }
  int j = 0, flag = 0;
  for (int i = 1; i <= n; i++) {
    if (!book[i]) {
      book[i] = 1;
      int t = a[i], cnt = 1;
      while (t != i) {
        if (cnt > n) {
          flag = 1;
          break;
        }
        book[t] = 1;
        t = a[t];
        cnt++;
      }
      if (cnt & 1) {
        b[j++] = cnt;
      } else
        b[j++] = cnt >> 1;
    }
  }
  if (flag) {
    printf("-1\n");
    return 0;
  }
  int t = 0;
  if (j == 1) {
    printf("%d\n", b[0]);
    return 0;
  }
  t = b[0] * b[1] / gcd(b[0], b[1]);
  for (int i = 2; i < j; i++) {
    t = b[i] * t / gcd(t, b[i]);
  }
  printf("%d\n", t);
  return 0;
}
