#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
const int max_n = 105;
int a[max_n];
bool vis[max_n];
int n;
int gcd(int a, int b) {
  if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
}
int min_mutiple(int a, int b) { return a * b / gcd(max(a, b), min(a, b)); }
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
  }
  memset(vis, false, sizeof(vis));
  bool flag = true;
  int ans = 1;
  int tmp;
  int counter;
  for (int u = 1; u <= n && flag; u++) {
    if (vis[u]) continue;
    counter = 0;
    int v = a[u];
    while (!vis[v]) {
      vis[v] = true;
      counter++;
      if (v == u)
        break;
      else
        v = a[v];
    }
    if (v != u) {
      flag = false;
      continue;
    }
    if (counter % 2 == 1)
      tmp = counter;
    else
      tmp = counter / 2;
    ans = min_mutiple(ans, tmp);
  }
  if (flag)
    printf("%d\n", ans);
  else
    printf("-1\n");
  return 0;
}
