#include using namespace std; typedef long long ll; static const ll INF = (ll)4e18; struct SegmentTree { int n; vector st; SegmentTree(int _n): n(_n) { st.assign(4*n, INF); } void update(int p, ll val) { update(1,1,n,p,val); } ll query(int l, int r) { return query(1,1,n,l,r); } private: void update(int node, int L, int R, int p, ll val) { if (L == R) { st[node] = min(st[node], val); return; } int mid = (L+R)/2; if (p <= mid) update(node*2, L, mid, p, val); else update(node*2+1, mid+1, R, p, val); st[node] = min(st[node*2], st[node*2+1]); } ll query(int node, int L, int R, int i, int j) { if (j < L || R < i) return INF; if (i <= L && R <= j) return st[node]; int mid = (L+R)/2; return min(query(node*2, L, mid, i, j), query(node*2+1, mid+1, R, i, j)); } }; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int N, Q; cin >> N >> Q; SegmentTree segL(N), segR(N); while (Q--) { int type; cin >> type; if (type == 1) { int x; cin >> x; // walk cost ll ans = (ll)x - 1; // teleport then walk from y <= x ll v1 = segL.query(1, x); if (v1 < INF) ans = min(ans, v1 + x); // teleport then walk from y >= x ll v2 = segR.query(x, N); if (v2 < INF) ans = min(ans, v2 - x); cout << ans << '\n'; } else { int x; ll c; cin >> x >> c; // insert teleporter at x with cost c segL.update(x, c - x); segR.update(x, c + x); } } return 0; }