#include <bits/stdc++.h>
using namespace std;
int vis[105] = {0}, c = 0, n;
vector<int> v[101];
int gcd(int a, int b) {
  if (a == b) return a;
  if (a > b) return gcd(a - b, b);
  return gcd(a, b - a);
}
int lcm(int a, int b) { return (a * b) / gcd(a, b); }
void dfs(int root, int x) {
  if (v[x][0] == root) {
    c++;
  } else if (v[x][0] != root && c < n) {
    if (!vis[v[x][0]])
      vis[v[x][0]] = 1;
    else {
      cout << -1;
      exit(0);
    }
    c++;
    dfs(root, v[x][0]);
  }
}
int main() {
  int ans = -1;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    v[i].push_back(x);
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      c = 0;
      vis[i] = 1;
      dfs(i, i);
      if (c <= n && ans == -1) {
        if (c % 2 == 0) c = c / 2;
        ans = c;
      } else if (c <= n) {
        if (c % 2 == 0) c = c / 2;
        ans = lcm(ans, c);
      } else {
        cout << -1;
        exit(0);
      }
    }
  }
  cout << ans;
}
