#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100 + 10;
int n;
int c[MAXN];
bool cal[MAXN];
bool vis[MAXN];
long long ans = 1;
int dfs(int num, int &d) {
  if (vis[num]) {
    return num;
  }
  vis[num] = true;
  d++;
  return dfs(c[num], d);
}
long long gcd(long long x, long long y) {
  if (x % y == 0) {
    return y;
  }
  return gcd(y, x % y);
}
int main(int argc, const char *argv[]) {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      int deep = 0;
      if (dfs(i, deep) != i) {
        cout << "-1" << endl;
        return 0;
      }
      if (deep % 2 == 0) {
        cal[deep / 2] = true;
      } else
        cal[deep] = true;
    }
  }
  for (int i = 2; i <= n; i++) {
    if (cal[i]) {
      ans = ans / gcd(ans, (long long)i) * (long long)i;
    }
  }
  cout << ans << endl;
  return 0;
}
