#include <bits/stdc++.h>
using namespace std;
int const N = 200002;
int const Q = 1100002;
int const A = 5100002;
int const MD = 1000000007;
int n, q;
int a[N], ans[Q];
vector<pair<int, int> > que[N];
int lp[A], pr[A / 10], sz;
int tr[N];
map<int, int> z[A];
inline void upd(int pos, int val) {
  for (int i = pos; i >= 0; i &= i + 1, --i) {
    if (tr[i] > val) tr[i] = val;
  }
}
inline int get(int l) {
  int ans = MD;
  for (int i = l; i < N; i |= i + 1) {
    if (ans > tr[i]) ans = tr[i];
  }
  return ans;
}
int main() {
  scanf("%d%d", &n, &q);
  for (int(i) = (1); (i) <= (n); ++(i)) scanf("%d", &a[i]);
  for (int(i) = (1); (i) <= (q); ++(i)) {
    int l, r;
    scanf("%d%d", &l, &r);
    que[r].push_back(make_pair(l, i));
  }
  for (int(i) = (2); (i) <= (A - 1); ++(i)) {
    if (!lp[i]) lp[i] = pr[sz++] = i;
    for (int j = 0; j < sz && i * pr[j] < A && pr[j] <= lp[i]; ++j)
      lp[i * pr[j]] = pr[j];
  }
  for (int(i) = (0); (i) <= (N - 1); ++(i)) tr[i] = MD;
  for (int(i) = (1); (i) <= (n); ++(i)) {
    int x = a[i];
    vector<int> f;
    while (x > 1) {
      int e = lp[x], c = 0;
      while (x % e == 0) {
        x /= e;
        c ^= 1;
      }
      if (c) f.push_back(e);
    }
    int L = 1 << int(f.size());
    for (int(msk) = (0); (msk) <= (L - 1); ++(msk)) {
      int cur = 1, cost = 0;
      for (int(bit) = (0); (bit) <= (int(f.size()) - 1); ++(bit)) {
        if (msk & (1 << bit))
          cur *= f[bit];
        else
          ++cost;
      }
      for (auto &it : z[cur]) {
        upd(it.second, it.first + cost);
      }
      z[cur][cost] = max(z[cur][cost], i);
    }
    for (auto &it : que[i]) {
      ans[it.second] = get(it.first);
    }
  }
  for (int(i) = (1); (i) <= (q); ++(i)) cout << ans[i] << "\n";
  return 0;
}
