#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
template <class T>
T f_max(T a, T b) {
  return a > b ? a : b;
}
template <class T>
T f_min(T a, T b) {
  return a < b ? a : b;
}
template <class T>
T f_abs(T a) {
  return a > 0 ? a : -a;
}
template <class T>
T gcd(T a, T b) {
  return b ? gcd(b, a % b) : a;
}
template <class T>
T lcm(T a, T b) {
  return a / gcd(a, b) * b;
}
template <class T>
void f_swap(T &a, T &b) {
  T c;
  c = a;
  a = b;
  b = c;
}
const int maxn = 100 + 50;
int crush[maxn], num[maxn], vis[maxn];
int main() {
  int n;
  int flag = 1;
  long long ans = 1;
  memset(num, 0, sizeof(num));
  memset(vis, 0, sizeof(vis));
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &crush[i]);
    num[crush[i]]++;
  }
  for (int i = 1; i <= n; i++)
    if (num[i] > 1) {
      flag = 0;
      break;
    }
  if (flag) {
    for (int i = 1; i <= n; i++) {
      if (vis[i]) continue;
      int cnt = 1, now = crush[i];
      vis[i] = 1;
      while (!vis[now]) {
        vis[now] = 1;
        cnt++;
        now = crush[now];
      }
      if (cnt & 1)
        ans = lcm(ans, (long long)cnt);
      else
        ans = lcm(ans, (long long)(cnt / 2));
    }
  }
  if (flag)
    printf("%lld\n", ans);
  else
    printf("-1\n");
  return 0;
}
