#include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
int n, a[maxn], vis[maxn];
int gcd(int a, int b) {
  while (b) {
    int c = a % b;
    a = b;
    b = c;
  }
  return a;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  int flag = 0;
  vector<int> ans;
  for (int i = 1; i <= n; i++) {
    if (vis[i]) continue;
    int now = i;
    int cnt = 0;
    while (!vis[now]) {
      vis[now] = i;
      now = a[now];
      cnt++;
    }
    if (now != i) return puts("-1"), 0;
    if (cnt % 2 == 0) cnt /= 2;
    ans.push_back(cnt);
  }
  long long A = ans[0];
  for (int i = 1; i < ans.size(); i++) A = A * ans[i] / gcd(A, ans[i]);
  cout << A << endl;
}
