#include <bits/stdc++.h>
using namespace std;
long long N, adj[111], vis[111], com, siz[111], ans = 1;
long long dfs(long long n, long long s, long long d, long long z) {
  if (vis[n] == z) {
    siz[z] = d;
    return n != s;
  }
  if (vis[n]) return 1;
  vis[n] = z;
  return dfs(adj[n], s, d + 1, z);
}
long long gcd(long long a, long long b) {
  for (long long c; b;) {
    c = b;
    b = a % b;
    a = c;
  }
  return a;
}
void no() {
  cout << -1 << "\n";
  exit(0);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> N;
  for (int i = 1; i <= N; i++) cin >> adj[i];
  for (int i = 1; i <= N; i++)
    if (!vis[i] && dfs(i, i, 0, ++com)) no();
  for (int i = 1; i <= com; i++)
    if (siz[i] % 2 == 0) siz[i] /= 2;
  for (int i = 1; i <= com; i++) ans = ans * siz[i] / gcd(ans, siz[i]);
  cout << ans << "\n";
}
