#include <bits/stdc++.h>
using namespace std;
const int MAXN = 105;
bool flag[MAXN];
int a[MAXN], n, tot, ans, d[MAXN];
long long cnt[MAXN];
void dfs(int now) {
  if (flag[now]) return;
  flag[now] = 1;
  ++ans;
  dfs(a[now]);
}
long long gcd(long long a, long long b) {
  if (!b)
    return a;
  else
    return gcd(b, a % b);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d\n", &a[i]);
    d[a[i]]++;
  }
  for (int i = 1; i <= n; i++)
    if (!d[i]) {
      printf("-1\n");
      return 0;
    }
  for (int i = 1; i <= n; i++) {
    if (flag[i]) continue;
    ans = 0;
    dfs(i);
    cnt[++tot] = ans;
  }
  long long now = 1;
  for (int i = 1; i <= tot; i++) {
    now = now * cnt[i] / gcd(now, cnt[i]);
  }
  if (now % 2 == 0) now = now / 2;
  printf("%I64d", now);
}
