#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<long long>;
using vl = vector<long long>;
using pii = pair<long long, long long>;
using pll = pair<long long, long long>;
const long long DX[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0},
                DY[9] = {-1, 0, 1, 0, -1, 1, 1, -1, 0};
const long long INF = 1e9 * 2;
const long long INFLL = 1e18 * 2;
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto i = begin(v); i != end(v); ++i)
    os << *i << (i == end(v) - 1 ? "" : " ");
  return os;
}
template <class T>
istream &operator>>(istream &is, vector<T> &v) {
  for (auto i = begin(v); i != end(v); ++i) is >> *i;
  return is;
}
template <class T>
istream &operator>>(istream &is, pair<T, T> &p) {
  is >> p.second >> p.first;
  return is;
}
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
struct node {
  vi out;
  vi in;
};
ll gcd(ll a, ll b) { return a ? gcd(b % a, a) : b; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
signed main() {
  long long n;
  cin >> n;
  vi crush(n);
  vector<node> g(n);
  for (long long i = 0, i_len = (long long)n; i < i_len; ++i) {
    cin >> crush[i];
    crush[i]--;
    g[i].out.push_back(crush[i]);
    g[crush[i]].in.push_back(i);
  }
  for (long long i = 0, i_len = (long long)n; i < i_len; ++i) {
    if (((long long)g[i].in.size()) == 0) {
      cout << -1 << endl;
      return 0;
    }
  }
  vector<bool> reached(n, false);
  long long ans = 1;
  for (long long i = 0, i_len = (long long)n; i < i_len; ++i) {
    if (reached[i]) continue;
    reached[i] = true;
    long long v = crush[i];
    long long t = 1;
    while (true) {
      if (v == i) break;
      reached[v] = true;
      v = crush[v];
      t++;
    }
    if (t % 2 == 0) t /= 2;
    ans = lcm(ans, t);
  }
  cout << ans << endl;
  return 0;
}
