#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
long long powmod(long long a, long long b, long long p) {
  long long res = 1;
  a %= p;
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % p;
    a = a * a % p;
  }
  return res;
}
template <typename T>
T gcd(T x, T y) {
  while (y) {
    T t = x % y;
    x = y;
    y = t;
  }
  return x;
}
template <typename T>
T LCM(T x, T y) {
  return x / gcd(x, y) * y;
}
template <typename T>
inline void add(T &x, T y) {
  x += y;
  if (x >= 1000000007) x -= 1000000007;
}
template <typename T>
inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
int read() {
  int ans = 0;
  char last = ' ', ch = getchar();
  while (ch >= '0' && ch <= '9') ans = ans * 10 + ch - '0', ch = getchar();
  return ans;
}
int n, m;
int a[2510], b[2510];
int ret = 1, flag = 1;
void fckn(int p) {
  int cnt = 0, r = p;
  while (b[p] == 0) {
    b[p] = 1;
    p = a[p];
    ++cnt;
  }
  if (p != r)
    flag = 0;
  else {
    if (!(cnt % 2)) cnt /= 2;
    ret = ret / gcd(cnt, ret) * cnt;
  }
}
void work() {
  for (int i = 1; i <= n; i++)
    if (!b[i]) {
      fckn(i);
      if (!flag) break;
    }
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  work();
  if (!flag)
    puts("-1");
  else
    cout << ret << endl;
  return 0;
}
