#include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
int n, cnt, adj[maxn], b[maxn];
bool mark[maxn];
long long res = 1;
void dfs(int u) {
  mark[u] = 1;
  ++b[cnt];
  if (!mark[adj[u]]) dfs(adj[u]);
}
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &adj[i]);
    ++b[adj[i]];
  }
  for (int i = 1; i <= n; ++i) {
    if (b[i] > 1) return puts("-1");
    b[i] = 0;
  }
  for (int i = 1; i <= n; ++i) {
    if (!mark[i]) {
      ++cnt;
      dfs(i);
    }
  }
  for (int i = 1; i <= cnt; ++i)
    if (b[i] % 2 == 0) b[i] /= 2;
  for (int i = 1; i <= cnt; ++i) res = res * b[i] / gcd(res, b[i]);
  cout << res;
}
