#include <bits/stdc++.h>
using namespace std;
long long root, sz, n, g[111], cnt, ans = 1;
bool used[111];
vector<int> c;
void dfs(int v) {
  used[v] = 1;
  if (!used[g[v]]) sz++, dfs(g[v]);
  if (g[v] == root) {
    if (g[root] != v) sz++;
    if (sz % 2 == 0) sz /= 2;
    c.push_back(max(sz, 1ll));
  }
}
long long gcd(long long a, long long b) {
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    g[i] = x;
  }
  for (int i = 1; i <= n; i++) {
    if (!used[i]) {
      sz = 0;
      root = i;
      dfs(i);
      cnt++;
    }
  }
  if (cnt != c.size()) {
    cout << "-1";
    exit(0);
  }
  for (int i = 0; i < c.size(); i++) {
    ans = lcm(ans, c[i]);
  }
  cout << ans;
}
