#include <bits/stdc++.h>
using namespace std;
const int max_n = 100;
int a[max_n], tmp[max_n];
bool vis[max_n];
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < (n); i++) {
    cin >> a[i];
    a[i]--;
    tmp[i] = a[i];
  }
  sort(tmp, tmp + n);
  for (int i = 0; i < (n); i++)
    if (i != tmp[i]) {
      cout << -1 << endl;
      return 0;
    }
  long long ans = 1;
  memset(vis, false, sizeof vis);
  for (int i = 0; i < (n); i++)
    if (!vis[i]) {
      int cnt = 0;
      for (int k = i; !vis[k]; k = a[k]) {
        vis[k] = true;
        cnt++;
      }
      if (cnt % 2 == 0) cnt /= 2;
      ans *= cnt / gcd(ans, cnt);
    }
  cout << ans << endl;
  return 0;
}
