#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
const double pi = acos(-1.0);
const int maxn = 200000 + 10;
const int mod = 1e9 + 7;
inline char _getchar() {
  static const int BUFSIZE = 100001;
  static char buf[BUFSIZE];
  static char *psta = buf, *pend = buf;
  if (psta >= pend) {
    psta = buf;
    pend = buf + fread(buf, 1, BUFSIZE, stdin);
    if (psta >= pend) return -1;
  }
  return *psta++;
}
inline int read(int &x) {
  x = 0;
  int f = 1;
  char ch = _getchar();
  while ((ch < '0' || ch > '9') && ~ch) {
    if (ch == '-') f = -1;
    ch = _getchar();
  }
  if (ch == -1) return -1;
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = _getchar();
  }
  x *= f;
  return 1;
}
inline int read(long long &x) {
  x = 0;
  int f = 1;
  char ch = _getchar();
  while ((ch < '0' || ch > '9') && ~ch) {
    if (ch == '-') f = -1;
    ch = _getchar();
  }
  if (ch == -1) return -1;
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = _getchar();
  }
  x *= f;
  return 1;
}
inline int read(double &x) {
  char in;
  double Dec = 0.1;
  bool IsN = false, IsD = false;
  in = _getchar();
  if (in == EOF) return -1;
  while (in != '-' && in != '.' && (in < '0' || in > '9')) in = _getchar();
  if (in == '-') {
    IsN = true;
    x = 0;
  } else if (in == '.') {
    IsD = true;
    x = 0;
  } else
    x = in - '0';
  if (!IsD) {
    while (in = _getchar(), in >= '0' && in <= '9') {
      x *= 10;
      x += in - '0';
    }
  }
  if (in != '.') {
    if (IsN) x = -x;
    return 1;
  } else {
    while (in = _getchar(), in >= '0' && in <= '9') {
      x += Dec * (in - '0');
      Dec *= 0.1;
    }
  }
  if (IsN) x = -x;
  return 1;
}
inline int read(float &x) {
  char in;
  double Dec = 0.1;
  bool IsN = false, IsD = false;
  in = _getchar();
  if (in == EOF) return -1;
  while (in != '-' && in != '.' && (in < '0' || in > '9')) in = _getchar();
  if (in == '-') {
    IsN = true;
    x = 0;
  } else if (in == '.') {
    IsD = true;
    x = 0;
  } else
    x = in - '0';
  if (!IsD) {
    while (in = _getchar(), in >= '0' && in <= '9') {
      x *= 10;
      x += in - '0';
    }
  }
  if (in != '.') {
    if (IsN) x = -x;
    return 1;
  } else {
    while (in = _getchar(), in >= '0' && in <= '9') {
      x += Dec * (in - '0');
      Dec *= 0.1;
    }
  }
  if (IsN) x = -x;
  return 1;
}
inline int read(char *x) {
  char *tmp = x;
  char in = _getchar();
  while (in <= ' ' && in != EOF) in = _getchar();
  if (in == -1) return -1;
  while (in > ' ') *(tmp++) = in, in = _getchar();
  *tmp = '\0';
  return 1;
}
int pos[5032108][8];
int a[maxn];
vector<pair<int, int> > val[16];
vector<pair<int, int> > op[16];
int kb[1 << 8];
int ones[1 << 8];
int ans[1049658 + 10];
int anss[1049658 + 10];
int tree[maxn << 2];
int q[((1049658 + 10) << 1) + maxn * 8];
int L[1049658 + 10], R[1049658 + 10];
void upd(int l, int r, int pos, int x, int v) {
  tree[pos] = min(tree[pos], v);
  if (l == r) return;
  int mid = (l + r) >> 1;
  if (x <= mid)
    upd(l, mid, pos << 1, x, v);
  else
    upd(mid + 1, r, pos << 1 | 1, x, v);
}
int query(int l, int r, int pos, int l1, int r1) {
  if (l >= l1 && r1 >= r) {
    return tree[pos];
  }
  int mid = (l + r) >> 1, ret = inf;
  if (mid >= l1) ret = query(l, mid, pos << 1, l1, r1);
  if (mid + 1 <= r1) ret = min(query(mid + 1, r, pos << 1 | 1, l1, r1), ret);
  return ret;
}
vector<pair<int, int> > A = val[0];
vector<pair<int, int> > B = op[0];
bool cmp(int a, int b) {
  if (A[a].second == A[b].second) return B[a] < B[b];
  return A[a].second < A[b].second;
}
void slove(int len) {
  memset(anss, 0, sizeof(anss));
  memset(tree, 0x3f, sizeof(tree));
  for (int i = 0; i < A.size(); i++) {
    q[i] = i;
  }
  int n = A.size();
  sort(q, q + n, cmp);
  for (int i = 0; i < n; i++) {
    int tr = B[q[i]].first;
    if (!tr) {
      upd(1, len, 1, A[q[i]].first, B[q[i]].second);
    } else {
      ans[tr] = query(1, len, 1, L[tr], R[tr]);
    }
  }
}
int main() {
  for (int i = 1; i < (1 << 8); i++) ones[i] = ones[i - (i & (-i))] + 1;
  kb[0] = 1;
  int n, q;
  read(n), read(q);
  for (int i = 1; i <= n; i++) {
    read(a[i]);
    int tmp = a[i];
    vector<int> k;
    int mx[15] = {0};
    for (int j = 2; j * j <= tmp; j++) {
      int cnt = 0;
      while (tmp % j == 0) {
        tmp /= j;
        cnt ^= 1;
      }
      if (cnt) k.push_back(j);
    }
    if (tmp != 1) k.push_back(tmp);
    int sz = k.size();
    for (int j = 0; j < sz; j++) kb[1 << j] = k[j];
    for (int j = 0; j < (1 << sz); j++) {
      kb[j] = kb[j - (j & (-j))] * kb[j & -j];
      int va = sz - ones[j];
      for (int w = 0; w < 8; w++) {
        if (pos[kb[j]][w]) {
          mx[w + va] = max(mx[w + va], pos[kb[j]][w]);
        }
      }
      pos[kb[j]][va] = i;
    }
    for (int j = 0; j <= 14; j++) {
      if (mx[j]) A.push_back(make_pair(mx[j], i)), B.push_back(make_pair(0, j));
    }
  }
  for (int i = 1; i <= q; i++) {
    read(L[i]), read(R[i]);
  }
  for (int j = 1; j <= q; j++) {
    A.push_back(make_pair(L[j], R[j])), B.push_back(make_pair(j, 1));
  }
  slove(n);
  for (int i = 1; i <= q; i++) printf("%d\n", ans[i]);
  return 0;
}
