#include <bits/stdc++.h>

using namespace std;
void fast_io() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
}

int main() {
    fast_io();
    int n, q;
    cin >> n >> q;
    for (; q--;) {
        long long s, t;
        cin >> s >> t;
        s += (1LL << n);
        t += (1LL << n);
        int ans = 0;
        while (s < t) {
            if (s & 1) {
                s++;
                ans++;
            }
            if (t & 1) {
                t--;
                ans++;
            }
            s >>= 1;
            t >>= 1;
        }
        cout << ans << "\n";
    }
}