#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a, long long int b) {
  if (!b) return a;
  return gcd(b, a % b);
}
long long int lcm(long long int a, long long int b) {
  long long int temp = gcd(a, b);
  return temp ? (a / temp * b) : 0;
}
vector<int> adj[105];
int vis[105];
int cnt, tmp;
bool flag;
void dfs(int node) {
  vis[node] = 1;
  if (node == tmp && cnt > 0) {
    flag = true;
    return;
  }
  if (cnt > 105) return;
  for (int i = 0; i < adj[node].size(); i++) {
    int v = adj[node][i];
    cnt++;
    dfs(v);
  }
  return;
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a;
    adj[i].push_back(a);
  }
  vector<int> v;
  for (int i = 1; i <= n; i++) {
    cnt = 0;
    flag = false;
    tmp = i;
    if (vis[i]) continue;
    dfs(i);
    if (flag) {
      if (cnt % 2 == 0) cnt /= 2;
      v.push_back(cnt);
    } else {
      cout << -1;
      return 0;
    }
  }
  long long int ans = std::accumulate(v.begin(), v.end(), 1, lcm);
  cout << ans << '\n';
  return 0;
}
