#include <bits/stdc++.h>
using namespace std;
int n;
int a[110];
int vis[110];
int dfs(int i, int x) {
  vis[i]++;
  if (vis[i] > 2) return -0x3f3f3f3f;
  if (a[i] == i && x != i) return -0x3f3f3f3f;
  if (a[i] == x) {
    vis[x]++;
    return 1;
  }
  return dfs(a[i], x) + 1;
}
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int main() {
  while (~scanf("%d", &n)) {
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    bool flag = 0;
    int ans = 1;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) vis[j] = 0;
      int t = dfs(i, i);
      if (t < 0) {
        flag = 1;
        break;
      }
      if (t % 2 == 0) t /= 2;
      if (ans == 0)
        ans = t;
      else if (ans != t && t != 0)
        ans = ans / gcd(ans, t) * t;
    }
    if (flag)
      printf("-1\n");
    else
      printf("%d\n", ans);
  }
  return 0;
}
