#include <bits/stdc++.h>
using namespace std;
const int N = 100 + 5;
int n, crush[N], t;
long long ret;
long long gcd(long long a, long long b) {
  if (!b) return a;
  return gcd(b, a % b);
}
void work(int u, int root, int step) {
  if (step && u == root) {
    t = step;
    return;
  }
  if (step > n) return;
  work(crush[u], root, step + 1);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &crush[i]);
  ret = 1;
  for (int i = 1; i <= n; i++) {
    t = -1;
    work(i, i, 0);
    if (t == -1) {
      printf("-1\n");
      return 0;
    }
    if (!(t & 1)) t >>= 1;
    ret = ret / gcd(ret, t) * t;
  }
  printf("%lld\n", ret);
  return 0;
}
