#include <bits/stdc++.h>
using namespace std;
vector<int> edge[101];
int visited[101], cycle[101], par[101], len = 1;
set<int> se;
int gcd(int a, int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
void dfs_visit(int myPar) {
  for (auto io : edge[myPar]) {
    if (!visited[io]) {
      visited[io] = 1;
      par[io] = myPar;
      dfs_visit(io);
    } else if (visited[io] != 2 && io == myPar) {
      cycle[io] = 1;
    } else if (visited[io] != 2) {
      int j = myPar, tempLen = 0;
      while (par[j] != io) {
        cycle[j] = 1;
        tempLen++;
        j = par[j];
      }
      cycle[j] = 1;
      cycle[io] = 1;
      tempLen += 2;
      if (tempLen % 2 == 0) tempLen /= 2;
      len = (len * tempLen) / (gcd(len, tempLen));
    }
  }
  visited[myPar] = 2;
}
void dfs(int n) {
  memset(visited, 0, sizeof(visited));
  memset(cycle, 0, sizeof(cycle));
  memset(par, 0, sizeof(par));
  for (int i = 1; i <= n; i++) {
    if (!visited[i]) {
      visited[i] = 1;
      par[i] = i;
      dfs_visit(i);
    }
  }
}
int main() {
  int n, i, j;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> j;
    edge[i].push_back(j);
  }
  dfs(n);
  for (i = 1; i <= n; i++)
    if (!cycle[i]) break;
  if (i > n) {
    cout << len << endl;
  } else
    cout << -1 << endl;
  return 0;
}
