#include <bits/stdc++.h>
using namespace std;
int maxx(int fd, int sd) { return (fd > sd) ? fd : sd; }
int minn(int fd, int sd) { return (fd < sd) ? fd : sd; }
long long gcd(long long a, long long b) {
  if (a == 0) return b;
  if (b == 0) return a;
  if (a < b)
    return gcd(b % a, a);
  else
    return gcd(a % b, b);
}
int main() {
  int n;
  scanf("%d\n", &n);
  vector<int> crush(n);
  vector<bool> loved(n, false);
  vector<int> looser(n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &crush[i]);
    crush[i]--;
    loved[crush[i]] = true;
  }
  for (int i = 0; i < n; ++i) {
    if (!loved[i]) {
      printf("-1");
      return 0;
    }
  }
  long long result = 1LL;
  vector<bool> used(n, false);
  for (int i = 0; i < n; ++i) {
    if (!used[i]) {
      int cur = i;
      long long len = 0;
      while (!used[cur]) {
        len++;
        used[cur] = true;
        cur = crush[cur];
      }
      if (len % 2 == 0) len /= 2;
      result = result * (len / gcd(result, len));
    }
  }
  printf("%I64d", result);
  return 0;
}
