#include <bits/stdc++.h>
using namespace std;
int n;
int q;
int msk[200002][20];
int cnt[200002];
vector<pair<int, int> > w[5032109];
vector<pair<int, int> > Q[200002];
int ans[2000002];
int idx[20];
int fff[200002];
int main() {
  for (int i = 0; i < 2000002; i++) {
    ans[i] = INT_MAX;
  }
  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    int val;
    scanf("%d", &val);
    for (int z = 2; z * z <= val; z++) {
      bool flag = false;
      while (val % z == 0) {
        flag ^= true;
        val /= z;
      }
      if (flag) {
        msk[i][cnt[i]++] = z;
      }
    }
    if (val > 1) {
      msk[i][cnt[i]++] = val;
    }
  }
  int id = -1;
  while (q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    l--;
    r--;
    Q[r].push_back(make_pair(l, ++id));
    ans[id] = INT_MAX;
  }
  memset(idx, -1, sizeof(idx));
  for (int i = 0; i < n; i++) {
    sort(Q[i].begin(), Q[i].end());
    for (int j = 0; j < (1 << cnt[i]); j++) {
      int val = 1;
      int er = 0;
      for (int k = 0; k < cnt[i]; k++) {
        if ((j >> k) & 1) {
          val *= msk[i][k];
        } else {
          er++;
        }
      }
      for (auto &lb : w[val]) {
        idx[lb.second + er] = max(idx[lb.second + er], lb.first);
      }
      while (w[val].size() && w[val].back().second >= er) {
        w[val].pop_back();
      }
      w[val].push_back(make_pair(i, er));
    }
    for (auto &g : Q[i]) {
      for (int z = 0; z < 15; z++) {
        if (idx[z] >= g.first) {
          ans[g.second] = z;
          break;
        }
      }
    }
  }
  for (int i = 0; i <= id; i++) {
    printf("%d\n", ans[i]);
  }
  return 0;
}
