#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int MX = 6000000;
const int N = 200000;
const int Q = 1200000;
int n, tests;
int pr[MX];
int ar[N];
int last_entry[MX][8];
int le[N][15];
vector<pair<int, int> > events[N];
vector<int> queries[N];
int t[N * 4];
int ans[Q];
void build(int v, int tl, int tr) {
  t[v] = 1e9;
  if (tl == tr) {
    return;
  }
  int sdfjahlfasfh = tl + tr;
  sdfjahlfasfh /= 2;
  build(v * 2, tl, sdfjahlfasfh);
  build(v * 2 + 1, sdfjahlfasfh + 1, tr);
}
int get(int v, int tl, int tr, int l, int r) {
  if (l > r) return 1e9;
  if (tl == l && tr == r) return t[v];
  int sdfjahlfasfh = tl + tr;
  sdfjahlfasfh /= 2;
  return min(get(v * 2, tl, sdfjahlfasfh, l, min(r, sdfjahlfasfh)),
             get(v * 2 + 1, sdfjahlfasfh + 1, tr, max(sdfjahlfasfh + 1, l), r));
}
void update(int v, int tl, int tr, int ps, int val) {
  if (tl == tr) {
    t[v] = min(t[v], val);
    return;
  }
  int sdfjahlfasfh = tl + tr;
  sdfjahlfasfh /= 2;
  if (ps <= sdfjahlfasfh)
    update(v * 2, tl, sdfjahlfasfh, ps, val);
  else
    update(v * 2 + 1, sdfjahlfasfh + 1, tr, ps, val);
  t[v] = min(t[v * 2], t[v * 2 + 1]);
}
vector<int> get_primes(int val) {
  vector<int> ret;
  while (val > 1) {
    int x = pr[val];
    int cnt = 0;
    while (val % x == 0) {
      val /= x;
      cnt++;
    }
    if (cnt % 2) ret.push_back(x);
  }
  return ret;
}
int l[Q], r[Q];
int naive(int l, int r) {
  int res = 1e9;
  for (int i = l; i <= r; i++) {
    for (int j = 0; j < 15; j++) {
      if (le[i][j] >= l) res = min(res, j);
    }
  }
  return res;
}
int main() {
  scanf("%d", &n);
  scanf("%d", &tests);
  pr[1] = 1;
  for (int i = 2; i < MX; i++) {
    if (pr[i] == 0) {
      for (int j = i; j < MX; j += i) {
        pr[j] = i;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &ar[i]);
  }
  for (int i = 1; i < MX; i++) {
    for (int j = 0; j <= 7; j++) {
      last_entry[i][j] = -1;
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= 15; j++) {
      le[i][j] = -1;
    }
    vector<int> v = get_primes(ar[i]);
    for (int mask = 0; mask < (1 << v.size()); mask++) {
      int val = 1;
      int pc = 0;
      for (int j = 0; j < v.size(); j++) {
        if (mask & (1 << j)) {
          val *= v[j];
          pc++;
        }
      }
      for (int q = pc; q <= 7; q++) {
        if (last_entry[val][q] == -1) continue;
        int cost = q + v.size() - 2 * pc;
        le[i][cost] = max(le[i][cost], last_entry[val][q]);
      }
    }
    for (int mask = 0; mask < (1 << v.size()); mask++) {
      int val = 1;
      int pc = 0;
      for (int j = 0; j < v.size(); j++) {
        if (mask & (1 << j)) {
          val *= v[j];
          pc++;
        }
      }
      last_entry[val][v.size()] = i;
    }
  }
  for (int test = 1; test <= tests; test++) {
    scanf("%d", &l[test]);
    scanf("%d", &r[test]);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < 15; j++) {
      if (le[i][j] != -1) {
        int ps = le[i][j];
        events[ps].push_back(make_pair(i, j));
      }
    }
  }
  for (int i = 1; i <= tests; i++) {
    queries[l[i]].push_back(i);
  }
  build(1, 1, n);
  for (int i = n; i >= 1; --i) {
    for (int j = 0; j < events[i].size(); j++) {
      int ps = events[i][j].first;
      int val = events[i][j].second;
      update(1, 1, n, ps, val);
    }
    for (int j = 0; j < queries[i].size(); j++) {
      int id = queries[i][j];
      int here = get(1, 1, n, l[id], r[id]);
      ans[id] = here;
    }
  }
  for (int i = 1; i <= tests; i++) {
    printf("%d\n", ans[i]);
  }
  return 0;
}
