#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
const int oo = 1e9;
const int nmax = 109;
int n;
int a[nmax];
bool b[nmax];
int ucln(int xx, int yy) {
  int x = xx;
  int y = yy;
  while (x != y) {
    if (x > y)
      x = x - y;
    else
      y = y - x;
  }
  return x;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  if (n == 0) {
    cout << 1;
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (b[a[i]]) {
      cout << -1;
      return 0;
    }
    b[a[i]] = true;
  }
  for (int i = 1; i <= n; i++) b[i] = false;
  int res = 0;
  for (int i = 1; i <= n; i++)
    if (!b[i]) {
      int t = 0;
      int j = i;
      while (!b[a[j]]) {
        b[a[j]] = true;
        t += 1;
        j = a[j];
      }
      if (t % 2 == 0) t = t / 2;
      if (res == 0)
        res = t;
      else
        res = (res * t) / ucln(res, t);
    }
  cout << res;
  return 0;
}
