#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  int c;
  while (a != 0) {
    c = a;
    a = b % a;
    b = c;
  }
  return b;
}
int lcm(int a, int b) { return a * b / gcd(a, b); }
int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, answer = 1;
  vector<int> crashes, visited;
  cin >> n;
  crashes.resize(n);
  visited.assign(n, 0);
  for (auto &i : crashes) cin >> i, --i;
  for (int i = 0; i < n; ++i) {
    if (visited[i] == 1) continue;
    visited[i] = -1;
    int j = i, s = 1;
    while (visited[crashes[j]] == 0) j = crashes[j], visited[j] = -1;
    int d = j;
    for (auto &_i : visited) _i = (_i == -1 ? 0 : _i);
    if (visited[crashes[j]] == 1) continue;
    visited[d] = 1;
    d = crashes[d];
    while (d != j) ++s, visited[d] = 1, d = crashes[d];
    if (s % 2 == 0) {
      answer = lcm(answer, s / 2);
    } else {
      answer = lcm(answer, s);
    }
  }
  for (auto i : visited) {
    if (i == 0) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << answer << endl;
  return 0;
}
