#include <bits/stdc++.h>
using namespace std;
int read() {
  char ch = getchar();
  int f = 0;
  while (ch < '0' || ch > '9') ch = getchar();
  while (ch >= '0' && ch <= '9') {
    f = (f << 1) + (f << 3) + ch - '0';
    ch = getchar();
  }
  return f;
}
int a[100005];
bool vis[100005];
long long ans = 1;
long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }
int main() {
  int n = read();
  bool ac = 1;
  for (int i = 1; i <= n; i++) a[i] = read();
  for (int i = 1; i <= n; i++) {
    if (vis[a[i]]) {
      printf("-1");
      return 0;
    }
    if (a[i] != i) ac = 0;
    vis[a[i]] = 1;
  }
  memset(vis, 0, sizeof(vis));
  for (int i = 1; i <= n; i++) {
    int now = i;
    if (!vis[now]) {
      long long size = 0;
      while (!vis[now]) {
        vis[now] = 1;
        size++;
        now = a[now];
      }
      if (size % 2 == 0) size /= 2;
      ans = (ans * size) / gcd(size, ans);
    }
  }
  cout << ans;
}
