#include <bits/stdc++.h>
using namespace std;
template <typename T>
bool chmax(T& a, const T& b) {
  return a < b ? (a = b, true) : false;
}
template <typename T>
bool chmin(T& a, const T& b) {
  return b < a ? (a = b, true) : false;
}
int N;
bool vis[101];
const int INF = 1e9;
long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
long long lcm(long long x, long long y) { return x / gcd(x, y) * y; }
long long dfs(int curr, int s, vector<int>& A) {
  if (vis[curr]) {
    return -INF;
  }
  vis[curr] = true;
  if (curr == s) {
    return 1;
  }
  return dfs(A[curr], s, A) + 1;
}
int main() {
  scanf("%d", &N);
  vector<int> A(N + 1);
  for (int i = 1; i <= N; i++) {
    scanf("%d", &A[i]);
  }
  long long min_loop = 1;
  for (int i = 1; i <= N; i++)
    if (vis[i] == false) {
      int tmp = dfs(A[i], i, A);
      if (tmp >= 1) {
        min_loop = lcm(min_loop, tmp);
      } else {
        printf("-1\n");
        return 0;
      }
    }
  if (min_loop % 2 == 0) min_loop /= 2;
  printf("%lld\n", min_loop);
  return 0;
}
