#include <bits/stdc++.h>
using namespace std;
const int N = 100;
int n, a[N + 1], deg[N + 1];
long long res = 1;
bool used[N + 1];
bool all_route() {
  for (int i = 1; i <= n; ++i)
    if (deg[i] != 1) return 0;
  return 1;
}
int dfs(int x) {
  used[x] = 1;
  if (used[a[x]]) return 1;
  return dfs(a[x]) + 1;
}
long long gcd(long long a, long long b) {
  long long m;
  while (b) {
    m = a % b;
    a = b;
    b = m;
  }
  return a;
}
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i], ++deg[a[i]];
  bool ok = all_route();
  if (!ok) {
    cout << -1;
    return 0;
  }
  for (int i = 1; i <= n; ++i)
    if (!used[i]) {
      int x = dfs(i);
      if (x % 2 == 0) x /= 2;
      res = lcm(res, x);
    }
  cout << res;
}
