#include <bits/stdc++.h>
using namespace std;
char READ_DATA;
int SIGNAL_INPUT;
template <typename Type>
inline char ru(Type &v) {
  SIGNAL_INPUT = 1;
  while ((READ_DATA = getchar()) < '0' || READ_DATA > '9')
    if (READ_DATA == '-')
      SIGNAL_INPUT = -1;
    else if (READ_DATA == EOF)
      return EOF;
  v = READ_DATA - '0';
  while ((READ_DATA = getchar()) >= '0' && READ_DATA <= '9')
    v = v * 10 + READ_DATA - '0';
  v *= SIGNAL_INPUT;
  return 1;
}
template <typename A, typename B>
inline char ru(A &x, B &y) {
  if (ru(x) == EOF) return EOF;
  ru(y);
  return 2;
}
template <typename A, typename B, typename C>
inline char ru(A &x, B &y, C &z) {
  if (ru(x) == EOF) return EOF;
  ru(y);
  ru(z);
  return 3;
}
template <typename A, typename B, typename C, typename D>
inline char ru(A &x, B &y, C &z, D &w) {
  if (ru(x) == EOF) return EOF;
  ru(y);
  ru(z);
  ru(w);
  return 4;
}
struct Edge {
  int v, next;
  Edge(int _v = 0, int nxt = -1) {
    v = _v;
    next = nxt;
  }
};
const int mod = 1e9 + 7;
const int maxn = 1e2 + 2;
int gcd(int a, int b) {
  if (!b) return a;
  return gcd(b, a % b);
}
int n, nxt[maxn];
int dfs(int s, int u) {
  int cnt = 1;
  while (cnt <= n + 1) {
    if (nxt[u] == s) return cnt;
    u = nxt[u];
    ++cnt;
  }
  return 0;
}
int main() {
  ru(n);
  for (int i = 1; i <= n; ++i) ru(nxt[i]);
  ;
  int ans = 1, tmp;
  for (int i = 1; i <= n; ++i) {
    tmp = dfs(i, i);
    if (!tmp) {
      printf("-1");
      return 0;
    }
    if ((tmp & 1) == 0) tmp /= 2;
    ans = ans / gcd(ans, tmp) * tmp;
  }
  printf("%d", ans);
}
