Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(M)]

class SegTreeMin:
    def __init__(self, n, inf=10 ** 15):
        self.n = n
        n2 = 1
        while n2 < n:
            n2 <<= 1
        self.n2 = n2
        self.tree = [inf] * (n2 << 1)
        self.inf = inf
        
    def update(self, i, x):
        i += self.n2
        self.tree[i] = x
        while i > 1:
            x = min(x, self.tree[i ^ 1])
            self.tree[i >> 1] = x
            i >>= 1
            
    def get_min(self, a, b):
        result = self.inf
        q = [(1, 0, self.n2)]
        while q:
            k, l, r = q.pop()
            if a <= l and r <= b:
                result = min(result, self.tree[k])
                continue
                
            m = (l + r) // 2
            k <<= 1
            if a < m and l < b:
                q.append((k, l, m))
            if a < r and l < m:
                q.append((k + 1, m, r))
                
        return result

INF = float("inf")
seg_tree = SegTreeMin(N, INF)
seg_tree.update(0, 0)
X.sort()
for l, r, c in X:
    l -= 1
    r -= 1
    tmp = seg_tree.get_min(l, r)
    seg_tree.update(r, min(tmp + c, seg_tree.get_min(r, r + 1)))
ans = seg_tree.get_min(N - 1, N)
print(ans if ans != INF else -1)


Fixed submission:
from heapq import heappop, heappush
N, M = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(M)]

edges = [[] for _ in range(N)]
for u, v, c in X:
    edges[u - 1].append((v - 1, c))
    
for i in range(N - 1):
    edges[i + 1].append((i, 0))

INF = float("inf")
d = [INF] * N
d[0] = 0

pq = []
heappush(pq, (0, 0))

while len(pq) > 0:
    cost, u = heappop(pq)
    for v, c in edges[u]:            
        if d[v] > d[u] + c:
            d[v] = d[u] + c
            heappush(pq, (d[v], v))
            
print(d[-1] if d[-1] != INF else -1)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(M)]

class SegTreeMin:
    def __init__(self, n, inf=10 ** 15):
        self.n = n
        n2 = 1
        while n2 < n:
            n2 <<= 1
        self.n2 = n2
        self.tree = [inf] * (n2 << 1)
        self.inf = inf
        
    def update(self, i, x):
        i += self.n2
        self.tree[i] = x
        while i > 1:
            x = min(x, self.tree[i ^ 1])
            self.tree[i >> 1] = x
            i >>= 1
            
    def get_min(self, a, b):
        result = self.inf
        q = [(1, 0, self.n2)]
        while q:
            k, l, r = q.pop()
            if a <= l and r <= b:
                result = min(result, self.tree[k])
                continue
                
            m = (l + r) // 2
            k <<= 1
            if a < m and l < b:
                q.append((k, l, m))
            if a < r and l < m:
                q.append((k + 1, m, r))
                
        return result

INF = 10 ** 15
seg_tree = SegTreeMin(N, INF)
seg_tree.update(0, 0)
X.sort()
for l, r, c in X:
    l -= 1
    r -= 1
    tmp = seg_tree.get_min(l, r)
    seg_tree.update(r, min(tmp + c, seg_tree.get_min(r, r + 1)))
ans = seg_tree.get_min(N - 1, N)
print(ans if ans != INF else -1)


Fixed submission:
from heapq import heappop, heappush
N, M = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(M)]

edges = [[] for _ in range(N)]
for u, v, c in X:
    edges[u - 1].append((v - 1, c))
    
for i in range(N - 1):
    edges[i + 1].append((i, 0))

INF = float("inf")
d = [INF] * N
d[0] = 0

pq = []
heappush(pq, (0, 0))

while len(pq) > 0:
    cost, u = heappop(pq)
    for v, c in edges[u]:            
        if d[v] > d[u] + c:
            d[v] = d[u] + c
            heappush(pq, (d[v], v))
            
print(d[-1] if d[-1] != INF else -1)