#include <bits/stdc++.h>
using namespace std;
long long n, crush[100005], ans = -1;
bool exi[100005];
long long ms(int o, int x, int p) {
  if (p > 2 * n) return -1;
  if (crush[x] == x) return p ? -1 : 0;
  if ((x == o) && p) return 0;
  int t = ms(o, crush[x], p + 1);
  return (t < 0 ? t : t + 1);
}
long long gcd(long long x, long long y) { return y == 0 ? x : gcd(y, x % y); }
long long lcm(long long x, long long y) { return x / gcd(x, y) * y; }
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &crush[i]);
    exi[crush[i]] = 1;
  }
  for (int i = 1; i <= n; i++) {
    if (!exi[i]) {
      ans = -1;
      break;
    }
    long long tmp = ms(i, i, 0);
    if (tmp <= 0) tmp = 1;
    tmp /= (2 - (tmp & 1));
    ans = ans < 0 ? tmp : lcm(ans, tmp);
  }
  cout << ans;
  return 0;
}
