#include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
int fa[maxn], sum[maxn], degree[maxn];
long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }
int Find(int x) {
  if (x == fa[x]) return fa[x];
  fa[x] = Find(fa[x]);
  return fa[x];
}
void Merge(int from, int to) {
  int x = Find(from);
  int y = Find(to);
  if (x != y) {
    fa[x] = y;
    sum[y] += sum[x];
  }
}
void init() {
  memset(degree, 0, sizeof(degree));
  for (int i = 0; i < maxn; i++) fa[i] = i, sum[i] = 1;
}
int main() {
  int n, to;
  while (scanf("%d", &n) != EOF) {
    init();
    for (int from = 1; from <= n; from++) {
      scanf("%d", &to);
      degree[from]++;
      degree[to]++;
      Merge(from, to);
    }
    for (int i = 1; i <= n; i++)
      if (degree[i] & 1) {
        printf("-1\n");
        return 0;
      }
    long long t = 1;
    for (int i = 1; i <= n; i++)
      if (fa[i] == i) {
        if (sum[i] & 1)
          t = t * sum[i] / gcd(t, sum[i]);
        else
          t = t * sum[i] / 2 / gcd(t, sum[i] / 2);
      }
    printf("%I64d\n", t);
  }
  return 0;
}
