#include <bits/stdc++.h>
using namespace std;
int i, now, n, a[105], nr;
long long rs = 1;
bool viz[105];
long long cmmdc(long long a, long long b) {
  while (a && b) (a > b ? a %= b : b %= a);
  return a + b;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (i = 1; i <= n; ++i) cin >> a[i];
  for (i = 1; i <= n; ++i) {
    if (viz[i]) continue;
    now = i;
    nr = 0;
    while (!viz[now]) {
      viz[now] = 1;
      now = a[now];
      ++nr;
    }
    if (now != i) return cout << "-1\n", 0;
    if (nr % 2 == 0) nr /= 2;
    rs = (rs * nr) / cmmdc(rs, nr);
  }
  cout << rs << '\n';
  return 0;
}
