#include <bits/stdc++.h>
using namespace std;
long long a[105], b[105];
bool stat[105];
long long gcd(long long x, long long y) {
  if (x % y == 0)
    return y;
  else
    return gcd(y, x % y);
}
int main() {
  long long n, i, j, k, x, t, g, mark;
  bool f;
  scanf("%lld", &n);
  for (i = 1; i <= n; i++) scanf("%lld", &a[i]);
  t = 1;
  f = 0;
  for (j = 1; j <= n; j++) {
    mark = 1;
    x = a[j];
    memset(stat, 0, sizeof(stat));
    while (x != j && stat[x] == 0) {
      stat[x] = 1;
      mark++;
      x = a[x];
    }
    if (x == j) {
      if (mark % 2 == 0) mark /= 2;
      g = gcd(t, mark);
      t = (t * mark) / g;
    } else
      f = 1;
  }
  if (f == 1)
    printf("-1\n");
  else
    printf("%lld\n", t);
  return 0;
}
