#include <bits/stdc++.h>
using namespace std;
int I, N, X[222], t, Sol;
bool mk[222];
void dfs(int x) {
  t++;
  if (X[x] == I) Sol = t;
  if (mk[X[x]]) return;
  mk[X[x]] = 1;
  dfs(X[x]);
}
int MCD(int u, int v) {
  int g = 1;
  while ((u % 2 == 0) && (v % 2 == 0)) {
    u /= 2;
    v /= 2;
    g *= 2;
  }
  while (u > 0) {
    if (u % 2 == 0)
      u /= 2;
    else if (v % 2 == 0)
      v /= 2;
    else if (u >= v)
      u -= v;
    else
      v -= u;
  }
  return g * v;
}
int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) {
    scanf("%d", &X[i]);
  }
  int GCD = 1;
  for (I = 1; I <= N; I++) {
    t = 0;
    Sol = 0;
    if (mk[I]) continue;
    mk[I] = 1;
    dfs(I);
    if (Sol) {
      if (!(Sol & 1)) {
        Sol /= 2;
      }
      GCD = (Sol * GCD) / MCD(Sol, GCD);
    } else {
      printf("-1\n");
      return 0;
    }
  }
  printf("%d\n", GCD);
}
