#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  for (;;) {
    if (a == 0) return b;
    b %= a;
    if (b == 0) return a;
    a %= b;
  }
}
int lcm(int a, int b) {
  int temp = gcd(a, b);
  return temp ? (a / temp * b) : 0;
}
list<int> adj[101];
int visited[101] = {0};
void add(int x, int y) { adj[x].push_back(y); }
int cyclce(int s) {
  int t = 1;
  int p = 0;
  queue<int> q;
  q.push(s);
  visited[s] = 1;
  list<int>::iterator it;
  while (!q.empty()) {
    int xt = q.front();
    q.pop();
    for (it = adj[xt].begin(); it != adj[xt].end(); it++) {
      if (*it == s) {
        p = 1;
        break;
      }
      if (visited[*it] == 0) {
        q.push(*it);
        t++;
        visited[*it] = 1;
      }
    }
    if (p == 1) break;
  }
  if (p == 0)
    t = -1;
  else if (t % 2 == 0)
    t = t / 2;
  return t;
}
int main() {
  int n, x;
  vector<int> v;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    add(i, x);
  }
  for (int i = 1; i <= n; i++) {
    if (visited[i] == 0) {
      int t = cyclce(i);
      if (t == -1) {
        cout << t;
        return 0;
      }
      v.push_back(t);
    }
  }
  int result = accumulate(v.begin(), v.end(), 1, lcm);
  cout << result;
  return 0;
}
