#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int a[N], cnt[N];
bool vis[N];
int gcd(long long a, int b) {
  while (a > 0 && b > 0) {
    if (a > b)
      a %= b;
    else
      b %= a;
  }
  return a + b;
}
long long lcm(long long a, int b) { return a / gcd(a, b) * b; }
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
  }
  for (int i = 1; i <= n; i++) {
    int x = i;
    memset(vis, false, sizeof(vis));
    while (vis[x] == false) {
      vis[x] = true;
      x = a[x];
      cnt[i]++;
    }
    if (cnt[i] % 2 == 0) cnt[i] >>= 1;
    if (x != i) {
      return puts("-1"), 0;
    }
  }
  long long ans = 1;
  for (int i = 1; i <= n; i++) {
    ans = lcm(ans, cnt[i]);
  }
  printf("%lld\n", ans);
}
