#include <bits/stdc++.h>
using namespace std;
const int inf = 2e8;
const int N = 6e6 + 10;
const int maxn = 200020;
vector<pair<int, int> > qry[maxn];
vector<int> vec;
int prime[N], tag[N], cnt, mn[N];
int id[N][8], a[maxn];
int n, q, ans[maxn * 10], rec[20];
void init() {
  for (register int i = 2; i <= 5040000; i++) {
    if (!tag[i]) prime[++cnt] = i, mn[i] = i;
    for (register int j = 1; j <= cnt; j++) {
      if (prime[j] * i > 5040000) break;
      tag[prime[j] * i] = 1;
      mn[prime[j] * i] = prime[j];
      if (i % prime[j] == 0) break;
    }
  }
}
void factor(int x) {
  vec.clear();
  while (x > 1) {
    int c = mn[x], cnt = 0;
    while (x % c == 0) x /= c, cnt++;
    if (cnt & 1) vec.push_back(c);
  }
}
void dfs(int n, int cur, int k, int pos) {
  if (n == vec.size()) {
    int c = vec.size();
    for (register int i = k; i <= 7; i++) {
      if (id[cur][i]) {
        rec[i + c - 2 * k] = max(rec[i + c - 2 * k], id[cur][i]);
      }
    }
    id[cur][c] = pos;
    return;
  }
  dfs(n + 1, cur * vec[n], k + 1, pos);
  dfs(n + 1, cur, k, pos);
}
void solve() {
  for (register int i = 1; i <= n; i++) {
    factor(a[i]);
    dfs(0, 1, 0, i);
    for (register int j = 0; j < (int)qry[i].size(); j++) {
      for (register int k = 0; k <= 14; k++) {
        if (qry[i][j].first <= rec[k]) {
          ans[qry[i][j].second] = k;
          break;
        }
      }
    }
  }
  for (register int i = 1; i <= q; i++) printf("%d\n", ans[i]);
}
int main() {
  init();
  scanf("%d %d", &n, &q);
  for (register int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (register int i = 1; i <= q; i++) {
    int l, r;
    scanf("%d %d", &l, &r);
    qry[r].push_back(make_pair(l, i));
  }
  solve();
}
