#include <bits/stdc++.h>
using namespace std;
namespace csl {
template <class T>
inline T gcd(T a, T b) {
  for (T c = T(); !!b;) c = a % b, a = std::move(b), b = std::move(c);
  return a;
}
template <class T>
T gcd(const T& a, const T& b, T& x, T& y) {
  if (!b) return x = 1, y = 0, a;
  T r = gcd(b, a % b, y, x);
  return y = y - a / b * x, r;
}
template <class T>
inline T lcm(const T& a, const T& b) {
  return a / gcd(a, b) * b;
}
template <class V, class E>
inline V mul(V a, E b, const V m) {
  V res = V();
  if (!a || !b) return res;
  for (; b; b >>= 1, a = (a + a) % m)
    if (b & 1) res = (res + a) % m;
  return res;
}
template <class V, class K>
inline V pow(V c, V n, K k) {
  for (; k; k >>= 1, n = n * n)
    if (k & 1) c = c * n;
  return c;
}
template <class V, class E>
inline V pow(V c, V n, E k, const V m) {
  for (c %= m; k; k >>= 1, n = (n * n) % m)
    if (k & 1) c = (c * n) % m;
  return c;
}
template <class V>
inline V inv(const V& x, const V& m) {
  V p, q;
  return gcd(x, m, p, q), (p % m + m) % m;
}
template <class V>
inline std::vector<V> divide(V x) {
  std::vector<V> res;
  if (x % 2 == 0) {
    res.push_back(2);
    while (x % 2 == 0) x >>= 1;
  }
  for (V i(3); i * i <= x; i += 2) {
    if (x % i) continue;
    res.push_back(i);
    while (x % i == 0) x /= i;
  }
  if (x != V(1)) res.push_back(x);
  return res;
}
template <class V>
inline V root(const V& P) {
  std::vector<V> p = csl::divide(P - 1);
  for (V g = 2; g < P; ++g) {
    bool flag = true;
    for (auto i = p.begin(); i != p.end(); ++i)
      if (csl::pow(V(1), g, (P - 1) / *i, P) == 1) {
        flag = false;
        break;
      }
    if (flag) return g;
  }
  return -1;
}
}  // namespace csl
const int N = 110;
int a[N], b[N], c[N];
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n;
  cin >> n;
  for (auto i = 1, i_n = n + 1; i < i_n; ++i) cin >> a[i];
  for (auto i = 1, i_n = n + 1; i < i_n; ++i) b[i] = i;
  for (auto k = 1, k_n = 200; k < k_n; ++k) {
    for (auto i = 1, i_n = n + 1; i < i_n; ++i) b[i] = a[b[i]];
    for (auto i = 1, i_n = n + 1; i < i_n; ++i)
      if (!c[i] && b[b[i]] == i) c[i] = k;
  }
  for (auto i = 1, i_n = n + 1; i < i_n; ++i)
    if (!c[i]) {
      cout << -1 << '\n';
      return 0;
    }
  int ans = 1;
  for (auto i = 1, i_n = n + 1; i < i_n; ++i) ans = csl::lcm(ans, c[i]);
  cout << ans << '\n';
  return 0;
}
