#include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
int crush[maxn], vis[maxn];
long long int step[maxn];
int n;
long long int gcd(long long int a, long long int b) {
  if (a < b) return gcd(b, a);
  if (b == 0) return a;
  return gcd(b, a % b);
}
void initial() {
  for (int i = 0; i < maxn; i++) {
    crush[i] = 0;
    step[i] = 0;
    vis[i] = 0;
  }
}
void readcase() {
  for (int i = 1; i <= n; i++) cin >> crush[i];
}
bool dfs(int deep, int u, int source) {
  if (vis[u]) {
    if (source != u) return false;
    if (deep % 2 == 0)
      step[source] = deep / 2;
    else
      step[source] = deep;
    return true;
  } else {
    vis[u] = 1;
    return dfs(deep + 1, crush[u], source);
  }
}
void computing() {
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < maxn; j++) vis[j] = 0;
    if (dfs(0, i, i) == false) {
      cout << -1 << endl;
      return;
    }
  }
  long long int ans = step[1];
  for (int i = 2; i <= n; i++) {
    ans = ans * step[i] / gcd(ans, step[i]);
  }
  cout << ans << endl;
}
int main() {
  while (cin >> n) {
    initial();
    readcase();
    computing();
  }
  return 0;
}
