#include <bits/stdc++.h>
using namespace std;
int n, a[109], vis[109];
vector<int> f;
int find(int x) {
  int now = x, ret = 0;
  while (1) {
    if (vis[now]) {
      if (now == x)
        return ret;
      else
        return -1;
    }
    ret++;
    vis[now] = 1;
    now = a[now];
  }
  return -1;
}
bool check(int x) {
  int now = a[x];
  for (int i = 1; i <= n; i++) {
    if (now == x) return 1;
    now = a[now];
  }
  return 0;
}
int gcd(int x, int y) {
  if (y == 0) return x;
  return gcd(y, x % y);
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++)
    if (check(i) == 0) {
      cout << -1 << endl;
      return 0;
    }
  for (int i = 1; i <= n; i++)
    if (!vis[i]) {
      int p = find(i);
      f.push_back(p);
    }
  sort(f.begin(), f.end());
  f.erase(unique(f.begin(), f.end()), f.end());
  int m = f.size();
  int now = f[0];
  for (int i = 1; i <= m - 1; i++) {
    int Next = gcd(now, f[i]);
    now = now * f[i] / Next;
  }
  if (now % 2)
    cout << now << endl;
  else
    cout << now / 2 << endl;
  return 0;
}
