#include <bits/stdc++.h>
using namespace std;
unsigned long long gcd(unsigned long long a, unsigned long long b) {
  unsigned long long r;
  while (b) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
int main() {
  int n, i, lg, j;
  bool ok[101] = {};
  unsigned long long rasp = 1;
  int a[101];
  bool b[101] = {};
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    ok[a[i]] = 1;
  }
  for (i = 1; i <= n; i++)
    if (ok[i] == 0) {
      cout << -1;
      return 0;
    }
  for (i = 1; i <= n; i++)
    if (b[i] == 0) {
      lg = 0;
      for (j = i; b[j] == 0; b[j] = 1, j = a[j], lg++)
        ;
      if (lg % 2 == 0) lg = lg / 2;
      rasp = rasp * lg / gcd(rasp, lg);
    }
  cout << rasp;
  return 0;
}
