// Idea: compress to a 20-node "bit graph".
// Put an edge between bit p and bit q if there exists some device having both bits p and q.
// Then the answer for (x, y) is:
// - 1 if (A[x] & A[y]) != 0
// - else min over p in bits(A[x]), q in bits(A[y]) of dist_bit[p][q] + 1
// If unreachable -> -1. (Max answer ≤ 20)
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
const int B = 20;
const int INF = 1e9;
int N, Q;
if (!(cin >> N >> Q)) return 0;
vector<int> A(N + 1);
for (int i = 1; i <= N; ++i) cin >> A[i];
// dist between bits
int dist[B][B];
for (int i = 0; i < B; ++i) {
for (int j = 0; j < B; ++j) dist[i][j] = (i == j ? 0 : INF);
}
// bits of each device
vector<array<int, B>> hasBit(N + 1);
vector<int> bitList[N + 1];
for (int i = 1; i <= N; ++i) {
int x = A[i];
for (int b = 0; b < B; ++b) {
if (x >> b & 1) bitList[i].push_back(b);
}
// connect all bits appearing together in this device
for (int p = 0; p < (int)bitList[i].size(); ++p)
for (int q = p + 1; q < (int)bitList[i].size(); ++q) {
int u = bitList[i][p], v = bitList[i][q];
dist[u][v] = dist[v][u] = 1;
}
}
// Floyd–Warshall on 20 nodes
for (int k = 0; k < B; ++k)
for (int i = 0; i < B; ++i)
for (int j = 0; j < B; ++j)
if (dist[i][k] + dist[k][j] < dist[i][j])
dist[i][j] = dist[i][k] + dist[k][j];
while (Q--) {
int x, y;
cin >> x >> y;
// quick checks
if ((A[x] & A[y]) != 0) {
cout << 1 << '\n';
continue;
}
if (bitList[x].empty() || bitList[y].empty()) {
cout << -1 << '\n';
continue;
}
int best = INF;
for (int p : bitList[x])
for (int q : bitList[y])
if (dist[p][q] < INF)
best = min(best, dist[p][q] + 1);
cout << (best >= INF ? -1 : best) << '\n';
}
return 0;
}