#include <bits/stdc++.h>
using namespace std;
template <class T>
T sqr(T x) {
  return x * x;
}
long double pi = 3.1415926535897932384626433832795;
const int N = 2e5 + 10, lg = 31, MAXN = 1111;
long long mod = 1e9 + 7;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long _gcd(long long a, long long b, long long& x, long long& y) {
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  }
  long long _x1, _y1;
  long long d = _gcd(b % a, a, _x1, _y1);
  x = _y1 - (b / a) * _x1;
  y = _x1;
  return d;
}
long long binpow(long long a, long long n) {
  long long res = 1;
  while (n) {
    if (n & 1) res *= a;
    a *= a;
    n >>= 1;
    a %= mod;
    res %= mod;
  }
  return res;
}
void term_res(string res) {
  cout << res;
  exit(0);
}
long long gcd2(long long a, long long b) {
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}
int c[111];
bool used[111];
bool came[111];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> c[i];
    c[i]--;
  }
  vector<int> cycle, tans;
  for (int i = 0; i < n; i++) {
    if (used[i]) continue;
    int cur = i;
    while (1) {
      cycle.push_back(cur);
      used[cur] = 1;
      came[c[cur]] = 1;
      cur = c[cur];
      if (used[cur]) break;
    }
    int a = cycle.size();
    if (a % 2 == 0) a /= 2;
    tans.push_back(a);
    cycle.clear();
  }
  bool ok = 1;
  for (int i = 0; i < n; i++) ok &= came[i];
  if (!ok) {
    cout << -1;
    return 0;
  }
  long long ans = 1;
  for (int i = 0; i < tans.size(); i++) {
    ans = ans * (tans[i]) / gcd2(ans, tans[i]);
  }
  cout << ans;
  return 0;
}
