#include <bits/stdc++.h>
using namespace std;
long long n;
long long a[105];
long long vis[105];
long long ans;
long long go(long long pos, long long dada) {
  if (vis[pos] != 0) {
    if (dada == pos)
      return 0;
    else
      return -1;
  }
  vis[pos] = -1;
  long long ret = go(a[pos], dada);
  if (ret == -1) return -1;
  return 1 + ret;
}
long long GCD(long long x, long long y) {
  if (y == 0) return x;
  return GCD(y, x % y);
}
long long LCM(long long x, long long y) {
  long long lcm = x * y / GCD(x, y);
  return lcm;
}
int main() {
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    scanf("%I64d", &a[i]);
  }
  ans = 1;
  for (long long i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      long long ret = go(i, i);
      if (ret == -1) {
        cout << -1;
        return 0;
      }
      ans = LCM(ans, ret);
    }
  }
  if (ans % 2 == 0) ans = ans / 2;
  cout << ans;
  return 0;
}
