#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 20;   const long long mod = 1e9 + 7;   int w[(maxn * 4) << 2], n;   vector<pair<int, int> > col[(maxn * 4) << 2];   int query(int L, int R, int l, int r, int rt);   void pushup(int rt) { w[rt] = w[rt << 1] + w[rt << 1 | 1]; }   void pushdown(int rt) {     for (int i = 0; i < col[rt].size(); i++) {       int r = col[rt][i].second, l = col[rt][i].first;       int m = (l + r) >> 1;       col[rt << 1].push_back(make_pair(r + l - m, r));       col[rt << 1 | 1].push_back(make_pair(l, r + l - (m + 1)));       w[rt << 1] += query(r + l - m, r, 0, n, 1);       w[rt << 1 | 1] += query(l, r + l - (m + 1), 0, n, 1);     }     col[rt].clear();   }   void build(int l, int r, int rt) {     col[rt].clear();     w[rt] = 1;     if (l == r) return;     int m = (l + r) >> 1;     build(l, m, rt << 1);     build(m + 1, r, rt << 1 | 1);     pushup(rt);   }   void update(int L0, int R0, int L, int R, int l, int r, int rt) {     if (L <= l && r <= R) {       int R1 = R0 - (l - L), L1 = L0 + R - r;       col[rt].push_back(make_pair(L1, R1));       w[rt] += query(L1, R1, 0, n, 1);       return;     }     pushdown(rt);     int m = (l + r) >> 1;     if (L <= m) update(L0, R0, L, R, l, m, rt << 1);     if (m < R) update(L0, R0, L, R, m + 1, r, rt << 1 | 1);     pushup(rt);   }   int query(int L, int R, int l, int r, int rt) {     if (L <= l && r <= R) {       return w[rt];     }     pushdown(rt);     int m = (l + r) >> 1;     int ret = 0;     if (L <= m) ret += query(L, R, l, m, rt << 1);     if (m < R) ret += query(L, R, m + 1, r, rt << 1 | 1);     return ret;   }   int main() {     int q;     while (~scanf( %d%d , &n, &q)) {       int curl = 0, curr = n - 1;       build(0, n, 1);       int ra = 1;       for (int i = 0; i < q; i++) {         int x;         scanf( %d , &x);         if (ra) {           if (x == 1) {             int p;             scanf( %d , &p);             if (curl + 2 * p - 1 <= curr) {               update(curl, curl + p - 1, curl + p, curl + 2 * p - 1, 0, n, 1);               curl += p;             } else {               p = curr - curl + 1 - p;               if (p)                 update(curr - p + 1, curr, curr - 2 * p + 1, curr - p, 0, n, 1);               curr -= p;               ra = 0;             }           }           if (x == 2) {             int l, r;             scanf( %d%d , &l, &r);             int ans = query(curl + l, curl + r - 1, 0, n, 1);             printf( %d n , ans);           }         } else {           if (x == 1) {             int p;             scanf( %d , &p);             if (curr - 2 * p + 1 >= curl) {               update(curr - p + 1, curr, curr - 2 * p + 1, curr - p, 0, n, 1);               curr -= p;             } else {               p = curr - curl + 1 - p;               if (p)                 update(curl, curl + p - 1, curl + p, curl + 2 * p - 1, 0, n, 1);               curl += p;               ra = 1;             }           }           if (x == 2) {             int l, r;             scanf( %d%d , &l, &r);             int ans = query(curr - r + 1, curr - l, 0, n, 1);             printf( %d n , ans);           }         }       }     }     return 0;   }