#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
int ne[maxn], vis[maxn];
const int inf = 0x3f3f3f3f;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &ne[i]);
  int maxx = 1;
  bool flag = true;
  memset(vis, 0, sizeof(vis));
  for (int i = 1; i <= n; i++) {
    int cnt = 1;
    int next = i;
    if (vis[next] == 0) {
      vis[next] = 1;
      while (1) {
        if (i == ne[next]) {
          int tmp = cnt % 2 == 0 ? cnt / 2 : cnt;
          maxx = maxx * tmp / gcd(maxx, tmp);
          break;
        }
        next = ne[next];
        cnt++;
        if (vis[next]) break;
        vis[next] = 1;
      }
      if (i != ne[next]) flag = false;
      if (!flag) break;
    }
  }
  if (!flag)
    puts("-1");
  else
    printf("%d\n", maxx);
  return 0;
}
