#include <bits/stdc++.h>
using ll = long long;
using ull = unsigned long long;
int gcd(int a, int b) {
  if (b == 0) return a;
  int r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}
int lcm(int a, int b) { return a * b / gcd(a, b); }
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);
  int N;
  std::cin >> N;
  std::vector<int> crush(N);
  std::vector<bool> marked(N, false);
  for (int i = 0; i < N; i++) {
    std::cin >> crush[i];
    crush[i]--;
  }
  int ans = 1;
  for (int i = 0; i < N; i++) {
    if (!marked[i]) {
      int length = 1, temp = i;
      marked[temp] = true;
      while (crush[temp] != i) {
        length++;
        if (marked[crush[temp]]) {
          std::cout << "-1";
          return 0;
        }
        temp = crush[temp];
        marked[temp] = true;
      }
      if (length % 2 == 0)
        ans = lcm(ans, length / 2);
      else
        ans = lcm(ans, length);
    }
  }
  std::cout << ans;
  return 0;
}
