#include <bits/stdc++.h>
using namespace std;
int n, a, maxe, maxo, cnt, score[101], restack[101], visited[101];
list<int> adj[101];
int DFS(int s) {
  visited[s] = 1;
  restack[s] = 1;
  cnt++;
  list<int>::iterator it;
  for (it = adj[s].begin(); it != adj[s].end(); it++) {
    if (!visited[*it])
      DFS(*it);
    else if (restack[*it])
      return cnt;
  }
  restack[s] = 0;
  return cnt;
}
int gcd(int p, int q) {
  int mi = min(p, q), ma = max(p, q);
  while (mi != 0) {
    int tmp = mi;
    mi = ma % mi;
    ma = tmp;
  }
  return ma;
}
int findlcm(int p, int q) { return p * (q / gcd(p, q)); }
int main() {
  ios::sync_with_stdio(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a;
    adj[i].push_back(a);
    score[a]++;
    if (score[a] > 1) return cout << -1, 0;
  }
  for (int i = 1; i <= n; i++)
    if (score[i] == 0) return cout << -1, 0;
  map<int, int> m;
  for (int i = 1; i <= n; i++) {
    if (!visited[i]) {
      cnt = 0;
      a = DFS(i);
      if (a % 2 == 0)
        m[a / 2] = 1;
      else
        m[a] = 1;
    }
  }
  map<int, int>::iterator it;
  int lcm = 1;
  for (it = m.begin(); it != m.end(); it++) {
    lcm = findlcm(lcm, it->first);
  }
  cout << lcm;
  return 0;
}
