#include <bits/stdc++.h>
using namespace std;
int n;
int c[100 + 1];
int check[100 + 1];
bool hasAns;
int ans;
long long ucln(long long a, long long b) {
  long long tmp;
  while (b > 0) {
    tmp = a;
    a = b;
    b = tmp % b;
  }
  return a;
}
long long bcnn(long long a, long long b) { return (a * b) / ucln(a, b); }
void solve() {
  int i, s, loop;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> c[i];
  }
  memset(check, 0, sizeof(check));
  hasAns = true;
  ans = 1;
  for (s = 1; s <= n && hasAns == true; s++) {
    if (check[s] == 0) {
      i = s;
      loop = 0;
      while (check[i] == 0) {
        loop++;
        check[i] = 1;
        i = c[i];
      }
      if (i != s) {
        hasAns = false;
      } else {
        if (loop % 2 == 0) loop = loop / 2;
        ans = bcnn(ans, loop);
      }
    }
  }
  if (hasAns == false) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}
int main() {
  solve();
  fflush(stdout);
  return 0;
}
