#include <bits/stdc++.h>
const int INF = 2000000005;
const long long BIG_INF = 2000000000000000005;
const int mod = 1000000007;
const int P = 31;
const long double PI = 3.141592653589793238462643;
const double eps = 1e-9;
using namespace std;
vector<pair<int, int> > dir = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
bool valid(int x, int y, int n, int m) {
  return x >= 0 && y >= 0 && x < n && y < m;
}
mt19937 rng(1999999973);
struct query {
  int l, r, ind;
  bool operator<(const query a) const { return r < a.r; }
};
const int N = 194598 + 1, Q = 1049658 + 1, V = 5032107 + 1;
query req[Q];
int a[N], dp[V][8], best[15], ans[Q];
vector<int> p, fact[N];
bool er[2500];
void precalc() {
  for (int i = 2; i < 2500; i++) {
    if (!er[i]) {
      p.push_back(i);
      for (int j = i * i; j < 2500; j += i) er[j] = true;
    }
  }
  for (int i = 0; i < V; i++) {
    for (int j = 0; j < 8; j++) dp[i][j] = -1;
  }
  for (int i = 0; i < 15; i++) best[i] = -1;
}
int n, q;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  precalc();
  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    int x = a[i];
    for (int j = 0; p[j] * p[j] <= a[i]; j++) {
      int col = 0;
      while (x % p[j] == 0) {
        col++;
        x /= p[j];
      }
      if (col % 2 == 1) fact[i].push_back(p[j]);
    }
    if (x != 1) fact[i].push_back(x);
  }
  for (int i = 0; i < q; i++) {
    cin >> req[i].l >> req[i].r;
    req[i].l--, req[i].r--;
    req[i].ind = i;
  }
  sort(req, req + q);
  int cur = 0;
  vector<pair<int, int> > div;
  for (int i = 0; i < n; i++) {
    div = {{1, 0}};
    int col = fact[i].size();
    for (int j = 0; j < fact[i].size(); j++) {
      for (int u = 0; u < (1 << j); u++)
        div.push_back({div[u].first * fact[i][j], div[u].second + 1});
    }
    for (int j = 0; j < div.size(); j++) {
      for (int u = 0; u < 8; u++) {
        best[col - div[j].second + u] =
            max(best[col - div[j].second + u], dp[div[j].first][u]);
      }
      dp[div[j].first][col - div[j].second] = i;
    }
    while (cur < q && req[cur].r == i) {
      for (int i = 0; i < 15; i++) {
        if (best[i] >= req[cur].l) {
          ans[req[cur].ind] = i;
          break;
        }
      }
      cur++;
    }
  }
  for (int i = 0; i < q; i++) cout << ans[i] << '\n';
  return 0;
}
