#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 9;
const double PI = acos(-1.0);
const double EPS = (1e-10);
const int dx4[4] = {0, 1, 0, -1};
const int dy4[4] = {-1, 0, 1, 0};
const int dx8[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const int dy8[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
bool cmp(int X, int Y) { return X > Y; }
int dcp(double A, double B) { return fabs(A - B) <= EPS ? 0 : A < B ? -1 : 1; }
long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; }
long long modpow(long long a, long long b, long long m) {
  a %= m;
  long long res = 1;
  while (b) {
    if (b & 1) res = (res * a) % m;
    a = (a * a) % m;
    b >>= 1;
  }
  return res;
}
long long bpow(long long a, long long b) {
  long long res = 1;
  while (b) {
    if (b & 1) res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}
long long modinv(long long a, long long m = MOD) { return modpow(a, m - 2, m); }
long long n;
vector<long long> adj[101];
long long visited[101];
long long edges = 0;
long long check;
void dfs(long long u) {
  if (visited[u]) {
    check = u;
    return;
  }
  visited[u] = 1;
  edges++;
  for (auto v : adj[u]) {
    dfs(v);
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    long long k;
    cin >> k;
    adj[i].push_back(k);
  }
  memset(visited, 0, sizeof(visited));
  long long ans = 1;
  for (long long i = 1; i <= n; i++) {
    if (!visited[i]) {
      edges = 0;
      dfs(i);
      if (check != i) {
        cout << -1 << '\n';
        return 0;
      }
    }
    long long t;
    if (edges % 2 == 0) {
      t = edges / 2;
    } else {
      t = edges;
    }
    ans = lcm(ans, t);
  }
  cout << ans;
}
