#include <bits/stdc++.h>
using namespace std;
int N;
int crush[105];
bool ok;
bool in[105];
int len;
long long all_len;
long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
int main() {
  scanf("%d", &N);
  for (int i = 0; i < (N); i++) scanf("%d", crush + i + 1);
  ok = true;
  for (int i = (1); i <= (N); i++) {
    if (in[crush[i]]) ok = false;
    in[crush[i]] = true;
  }
  if (!ok) {
    printf("-1\n");
    return 0;
  }
  all_len = 1;
  for (int i = (1); i <= (N); i++) {
    len = 1;
    int j = crush[i];
    while (j != i) {
      ++len;
      j = crush[j];
    }
    if (len % 2 == 0) len /= 2;
    all_len = lcm(all_len, len);
  }
  printf("%I64d\n", all_len);
  return 0;
}
