import sys input = sys.stdin.readline N, Q = map(int, input().split()) # テレポーター情報を格納 # teleporters[position] = cost teleporters = {} for _ in range(Q): query = list(map(int, input().split())) if query[0] == 1: # クエリ1: マス1からマスxへの最小コスト x = query[1] # 直接歩く場合のコスト min_cost = x - 1 # テレポーターを使う場合 for pos, tp_cost in teleporters.items(): # どこからでもテレポーターを使えるので、 # マス1からテレポーター位置まで歩く必要はない # テレポートでマスposに移動し、そこからマスxまで歩く total_cost = tp_cost + abs(x - pos) min_cost = min(min_cost, total_cost) print(min_cost) else: # クエリ2: マスxにコストcのテレポーター設置 x = query[1] c = query[2] # 既存のテレポーターがある場合は、より安いものに更新 if x not in teleporters: teleporters[x] = c else: teleporters[x] = min(teleporters[x], c)