#include <bits/stdc++.h>
using namespace std;
template <class T>
T min(T a, T b, T c) {
  return min(a, min(b, c));
}
template <class T>
T max(T a, T b, T c) {
  return max(a, max(b, c));
}
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int lcm(int a, int b) { return a * (b / gcd(a, b)); }
int n, a[1005], cnt[1005], visited[1005], ans;
void dfs(int u, int v) {
  ++cnt[u];
  visited[v] = 1;
  if (a[v] == u) {
    if (cnt[u] % 2 == 0)
      ans = lcm(ans, cnt[u] / 2);
    else
      ans = lcm(ans, cnt[u]);
    return;
  }
  if (visited[a[v]]) {
    ans = -1;
    return;
  }
  dfs(u, a[v]);
}
int main() {
  scanf("%d", &n);
  for (int i = (int)(1); i <= (int)(n); ++i) scanf("%d", &a[i]);
  ans = 1;
  for (int i = (int)(1); i <= (int)(n); ++i) {
    memset(visited, 0, sizeof visited);
    cnt[i] = 1;
    if (a[i] == i) continue;
    dfs(i, a[i]);
    if (ans == -1) break;
  }
  printf("%d\n", ans);
  return 0;
}
