#include <bits/stdc++.h>
using namespace std;
const int maxn = 100 + 10;
int d[maxn];
int in[maxn];
int r[maxn], vis[maxn];
int n, tail = 0;
long long ans = 1;
bool do_not_exist() {
  for (int i = 1; i <= n; i++)
    if (in[i] >= 2 || in[i] == 0) return 1;
  return 0;
}
void dfs(int x, int step, int s, int flag) {
  vis[x] = 1;
  if (x == s && flag == 1) {
    tail++;
    if (step % 2 == 1)
      r[tail] = step;
    else
      r[tail] = step / 2;
    return;
  } else {
    dfs(d[x], step + 1, s, 1);
  }
  return;
}
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &d[i]), in[d[i]]++;
  if (do_not_exist()) {
    printf("-1\n");
  } else {
    for (int i = 1; i <= n; i++) {
      if (vis[i] == 0) dfs(i, 0, i, 0);
    }
    for (int i = 1; i <= tail; i++) {
      long long now = r[i];
      long long cmp = ans * now;
      ans = cmp / gcd(ans, now);
    }
    cout << ans << endl;
  }
  return 0;
}
