#include <bits/stdc++.h>
using namespace std;
vector<int> v;
vector<vector<int> > g;
int edge;
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;
}
void dfsvisit(int i) {
  vector<int>::iterator it = g[i].begin();
  while (it != g[i].end()) {
    edge++;
    if (!v[*it]) {
      v[*it] = 1;
      dfsvisit(*it);
    }
    it++;
  }
}
void dfs() {
  int answer = 1;
  for (int i = 1; i < v.size() - 1; i++) {
    if (!v[i]) {
      edge = 0;
      v[i] = 1;
      dfsvisit(i);
      if ((edge / 2) % 2 == 1)
        answer = lcm(answer, (edge / 2));
      else
        answer = lcm(answer, edge / 4);
    }
  }
  cout << answer;
  return;
}
int main() {
  int n;
  cin >> n;
  int count[105] = {0};
  v.resize(n + 1);
  g.resize(n + 1);
  int a[n + 2];
  for (int i = 1; i < n + 1; i++) {
    scanf("%d", &a[i]);
    count[a[i]]++;
    g[i].push_back(a[i]);
    g[a[i]].push_back((i));
  }
  for (int i = 1; i < n + 1; i++) {
    if (count[i] == 0) {
      printf("-1");
      return 0;
    }
  }
  dfs();
  return 0;
}
