#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 9;
int arr[maxn];
int vis[maxn];
int n;
int mu;
long long re[111];
long long gcd(long long a, long long b) {
  long long c = 1;
  while (b) {
    c = a % b;
    a = b;
    b = c;
  }
  return a;
}
int sol(int a) {
  vis[a] = 1;
  int e = arr[a];
  if (vis[e]) {
    if (e == mu) {
      return 1;
    } else {
      return -1;
    }
  } else {
    int g = sol(e);
    if (g == -1)
      return -1;
    else
      return g + 1;
  }
}
int main() {
  int a, b, c, d, e, f, g, h;
  while (scanf("%d", &n) == 1) {
    for (d = 1; d <= n; d++) {
      scanf("%d", &arr[d]);
    }
    memset(vis, 0, sizeof(vis));
    g = 0;
    h = 0;
    a = 0;
    for (d = 1; d <= n; d++) {
      if (!vis[d]) {
        mu = d;
        e = sol(d);
        if (e == -1) {
          g = -1;
          break;
        }
        if (e % 2) {
          re[a++] = e;
        } else
          re[a++] = e / 2;
      }
    }
    if (g < 0) {
      printf("-1\n");
    } else {
      long long res = 1, tmp;
      for (d = 0; d < a; d++) {
        tmp = gcd(res, re[d]);
        res = res * re[d] / tmp;
      }
      cout << res << endl;
    }
  }
  return 0;
}
