#include <bits/stdc++.h> // NeOWami
using namespace std;
#define ft first
#define sc second
#define int long long
const int N = 1e5 + 5;
int k, q;
struct query {
int s, n, l, r, v, id;
} Q[N];
namespace sub1 {
bool checksub() {
for (int i = 1; i <= q; i++) if (Q[i].n > 5) return 0;
return 1;
}
void calc(int s, int n, int l, int r, int v) {
vector<int> cur; cur.push_back(s);
for (int t = 1; t <= n; t++) {
vector<int> nxt;
for (int i: cur) {
if (i < k) nxt.push_back(i + 1);
else {
for (int i = k; i >= 0; i--) nxt.push_back(i);
// nxt.push_back(-1);
}
}
cerr << t<< ": "; for (int i: nxt) cerr << i << " "; cerr << "\n";
swap(nxt, cur);
}
int cnt = 0;
for (int i = l; i <= r; i++) if (cur[i - 1] == v) cnt++;
cout << cnt << "\n";
}
void solve() {
for (int i = 1; i <= q; i++) {
calc(Q[i].s, Q[i].n, Q[i].l, Q[i].r, Q[i].v);
}
}
};
const int LIM = 1e18;
namespace sub3 {
int fibo[N], sz;
int cnt[N][2];
int getVal(int pos, int layer, int v) {
if (layer <= 0) return 0;
if (pos <= 0) return 0;
if (pos == fibo[layer]) return cnt[layer][v];
if (pos < fibo[layer - 1]) return getVal(pos, layer - 1, v);
return getVal(fibo[layer - 1], layer - 1, v) + getVal(pos - fibo[layer - 1], layer - 2, v);
}
void calc(int s, int n, int l, int r, int v) {
if (n == 0) {
cout << (s == v) << "\n";
return;
}
if (s == 0) n--;
cout << getVal(r, min(sz, n + 1), v) - getVal(l - 1, min(sz, n + 1), v) << "\n";
}
void solve() {
fibo[0] = 1, fibo[1] = 1;
cnt[0][0] = cnt[1][1] = 1;
for (int i = 2;; i++) {
fibo[i] = fibo[i - 1] + fibo[i - 2];
sz = i;
cnt[i][0] = cnt[i - 1][0] + cnt[i - 2][0];
cnt[i][1] = cnt[i - 1][1] + cnt[i - 2][1];
if (fibo[i] > LIM) break;
}
for (int i = 1; i <= q; i++) {
calc(Q[i].s, Q[i].n, Q[i].l, Q[i].r, Q[i].v);
}
// for (int i = 1; i <= sz; i++) {
// cerr << i << " " << fibo[i] << " " << cnt[i][0] << " " << cnt[i][1] << "\n";
// }
}
};
//1100087778366101931
namespace subfull {
int len[N], sz = 0;
int cnt[N][10];
void build() {
// memset(cnt, 0, sizeof(cnt));
for (int i = 0; i <= k; i++) {
len[i] = 1;
cnt[i][i] = 1;
cnt[k + 1][i] = 1;
}
len[k + 1] = k + 1;
int lastNum = 0;
sz = k + 1;
for (int i = k + 2;; i++) {
len[i] = len[i - 1] * 2 - 1;
for (int j = 0; j <= k; j++) cnt[i][j] = cnt[i - 1][j] * 2;
cnt[i][lastNum]--;
if (lastNum < k) lastNum++;
else lastNum = 0;
sz = i;
if (len[i] > LIM) break;
}
// for (int i = 0; i <= sz; i++) {
// cerr << i << ": " << len[i] << " | "; for (int j = 0; j <= k; j++) cerr << cnt[i][j] << " "; cerr << "\n";
// }
}
int getVal(int pos, int layer, int v) {
if (pos <= k) return (v > k - pos);
if (layer < 0) return 0;
if (pos == len[layer] || layer == 0) return cnt[layer][v];
if (pos < len[layer - 1]) return getVal(pos, layer - 1, v);
return getVal(len[layer - 1], layer - 1, v) + getVal(pos - len[layer - 1], layer - 1, v);
}
void calc(int s, int n, int l, int r, int v) {
n += s;
// cerr << n << ": " << getVal(l - 1, min(sz, n), v) << " - "<< getVal(r, min(sz, n), v) << "\n";
cout << getVal(r, min(sz, n), v) - getVal(l - 1, min(sz, n), v) << "\n";
}
void solve() {
build();
for (int i = 1; i <= q; i++) {
calc(Q[i].s, Q[i].n, Q[i].l, Q[i].r, Q[i].v);
}
}
};
signed main() {
cin.tie(NULL)->sync_with_stdio(false);
if(ifstream("transseq.inp")) {
freopen("transseq.inp", "r", stdin);
freopen("transseq.out", "w", stdout);
}
cin >> k >> q;
for (int i = 1; i <= q; i++) {
cin >> Q[i].s >> Q[i].n >> Q[i].l >> Q[i].r >> Q[i].v;
Q[i].id = i;
}
if (sub1::checksub()) return sub1::solve(), 0;
if (k == 1) return sub3::solve(), 0;
return subfull::solve(),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pbnQgY250ID0gMDsKICAgIGZvciAoaW50IGkgPSBsOyBpIDw9IHI7IGkrKykgaWYgKGN1cltpIC0gMV0gPT0gdikgY250Kys7CiAgICBjb3V0IDw8IGNudCA8PCAiXG4iOwp9CnZvaWQgc29sdmUoKSB7CiAgICBmb3IgKGludCBpID0gMTsgaSA8PSBxOyBpKyspIHsKICAgICAgICBjYWxjKFFbaV0ucywgUVtpXS5uLCBRW2ldLmwsIFFbaV0uciwgUVtpXS52KTsKICAgIH0KfQp9OwoKY29uc3QgaW50IExJTSA9IDFlMTg7Cm5hbWVzcGFjZSBzdWIzIHsKaW50IGZpYm9bTl0sIHN6OwppbnQgY250W05dWzJdOwppbnQgZ2V0VmFsKGludCBwb3MsIGludCBsYXllciwgaW50IHYpIHsKICAgIGlmIChsYXllciA8PSAwKSByZXR1cm4gMDsKICAgIGlmIChwb3MgPD0gMCkgcmV0dXJuIDA7CiAgICBpZiAocG9zID09IGZpYm9bbGF5ZXJdKSByZXR1cm4gY250W2xheWVyXVt2XTsKICAgIGlmIChwb3MgPCBmaWJvW2xheWVyIC0gMV0pIHJldHVybiBnZXRWYWwocG9zLCBsYXllciAtIDEsIHYpOwogICAgcmV0dXJuIGdldFZhbChmaWJvW2xheWVyIC0gMV0sIGxheWVyIC0gMSwgdikgKyBnZXRWYWwocG9zIC0gZmlib1tsYXllciAtIDFdLCBsYXllciAtIDIsIHYpOwp9CnZvaWQgY2FsYyhpbnQgcywgaW50IG4sIGludCBsLCBpbnQgciwgaW50IHYpIHsKICAgIGlmIChuID09IDApIHsKICAgICAgICBjb3V0IDw8IChzID09IHYpIDw8ICJcbiI7CiAgICAgICAgcmV0dXJuOwogICAgfQogICAgaWYgKHMgPT0gMCkgbi0tOwogICAgY291dCA8PCBnZXRWYWwociwgbWluKHN6LCBuICsgMSksIHYpIC0gZ2V0VmFsKGwgLSAxLCBtaW4oc3osIG4gKyAxKSwgdikgPDwgIlxuIjsKfQp2b2lkIHNvbHZlKCkgewogICAgZmlib1swXSA9IDEsIGZpYm9bMV0gPSAxOwogICAgY250WzBdWzBdID0gY250WzFdWzFdID0gMTsKICAgIGZvciAoaW50IGkgPSAyOzsgaSsrKSB7CiAgICAgICAgZmlib1tpXSA9IGZpYm9baSAtIDFdICsgZmlib1tpIC0gMl07CiAgICAgICAgc3ogPSBpOwogICAgICAgIGNudFtpXVswXSA9IGNudFtpIC0gMV1bMF0gKyBjbnRbaSAtIDJdWzBdOwogICAgICAgIGNudFtpXVsxXSA9IGNudFtpIC0gMV1bMV0gKyBjbnRbaSAtIDJdWzFdOwogICAgICAgIGlmIChmaWJvW2ldID4gTElNKSBicmVhazsKICAgIH0KICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IHE7IGkrKykgewogICAgICAgIGNhbGMoUVtpXS5zLCBRW2ldLm4sIFFbaV0ubCwgUVtpXS5yLCBRW2ldLnYpOwogICAgfQogICAgLy8gZm9yIChpbnQgaSA9IDE7IGkgPD0gc3o7IGkrKykgewogICAgLy8gICAgIGNlcnIgPDwgaSA8PCAiICIgPDwgZmlib1tpXSA8PCAiICIgPDwgY250W2ldWzBdIDw8ICIgIiA8PCBjbnRbaV1bMV0gPDwgIlxuIjsKICAgIC8vIH0KfQp9OwovLzExMDAwODc3NzgzNjYxMDE5MzEgCm5hbWVzcGFjZSBzdWJmdWxsIHsKaW50IGxlbltOXSwgc3ogPSAwOwppbnQgY250W05dWzEwXTsKdm9pZCBidWlsZCgpIHsKICAgIC8vIG1lbXNldChjbnQsIDAsIHNpemVvZihjbnQpKTsKICAgIGZvciAoaW50IGkgPSAwOyBpIDw9IGs7IGkrKykgewogICAgICAgIGxlbltpXSA9IDE7CiAgICAgICAgY250W2ldW2ldID0gMTsKICAgICAgICBjbnRbayArIDFdW2ldID0gMTsKICAgIH0KICAgIGxlbltrICsgMV0gPSBrICsgMTsKICAgIGludCBsYXN0TnVtID0gMDsKICAgIHN6ID0gayArIDE7CiAgICBmb3IgKGludCBpID0gayArIDI7OyBpKyspIHsKICAgICA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