#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e2 + 2;
int n, a[maxn], mark[maxn];
int dfs(int v) {
  mark[v] = 1;
  int ans = 1;
  if (!mark[a[v]]) ans += dfs(a[v]);
  return ans;
}
int gcd(int x, int y) {
  if (x < y) return gcd(y, x);
  if (x % y == 0) return y;
  return gcd(y, x % y);
}
int main() {
  cin >> n;
  int flag = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (mark[a[i]] == 1) flag = 1;
    mark[a[i]] = 1;
  }
  if (flag) {
    cout << -1;
    return 0;
  }
  int ans = 1;
  fill(mark, mark + 101, 0);
  for (int i = 1; i <= n; i++) {
    if (mark[i] != 1) {
      int v1 = dfs(i);
      if (!(v1 & 1)) v1 /= 2;
      ans = ans * v1 / gcd(ans, v1);
    }
  }
  cout << ans;
  return 0;
}
