#include <bits/stdc++.h>
using namespace std;
const int maxn = 100;
int n, nex[maxn + 5], cnt[maxn + 5];
int sta[maxn + 5], top;
char vis[maxn + 5];
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
int dfs(int x) {
  vis[x] = 1;
  if (vis[nex[x]]) return 1;
  int tmp = dfs(nex[x]);
  return tmp + 1;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", nex + i), ++cnt[nex[i]];
  for (int i = 1; i <= n; ++i) {
    if (!cnt[i]) {
      printf("-1\n");
      return 0;
    }
  }
  top = 0;
  for (int i = 1; i <= n; ++i) {
    if (vis[i]) continue;
    sta[++top] = dfs(i);
    if (sta[top] % 2 == 0) sta[top] /= 2;
  }
  long long ans = 1;
  for (int i = 1; i <= top; ++i) {
    long long tmp = gcd(ans, sta[i]);
    ans = ans / tmp * sta[i];
  }
  cout << ans << "\n";
  return 0;
}
