#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1000000000")
using namespace std;
const int maxn = 201;
long long color[maxn], tlen, ans = -1, x, n, a[maxn], p[maxn];
void dfs(int ver) {
  color[ver] = 1;
  tlen++;
  if (color[p[ver]]) return;
  dfs(p[ver]);
}
long long gcd(long long a, long long b) {
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x;
    p[i + 1] = x;
    a[x]++;
  }
  for (int i = 1; i <= n; i++) {
    if (!a[i]) {
      cout << -1;
      return 0;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (!color[i]) {
      tlen = 0;
      dfs(i);
      if (tlen % 2 == 0) {
        x = tlen / 2;
      } else {
        x = tlen;
      }
      if (ans == -1) ans = x;
      ans = lcm(ans, x);
    }
  }
  cout << ans;
  return 0;
}
