Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import defaultdict
from heapq import *

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    to = defaultdict(list)
    n, m = map(int, input().split())
    for _ in range(m):
        l, r, c = map(int, input().split())
        l, r = l - 1, r - 1
        to[l].append([r, c])
    costs = [-1] * n
    hp = []
    heappush(hp, [0, 0])
    while hp:
        #print(hp)
        c, u = heappop(hp)
        if u == n - 1:
            print(c)
            #print(costs)
            exit()
        if costs[u] != -1: continue
        costs[u] = c
        for v, dc in to[u]:
            if costs[v] != -1: continue
            heappush(hp, [c + dc, v])
            for w in range(v - 1, u, -1):
                if costs[w] != -1: break
                heappush(hp, [c + dc, w])

    print(-1)

main()


Fixed submission:
import sys
from collections import defaultdict
from heapq import *

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    to = defaultdict(list)
    n, m = map(int, input().split())
    for _ in range(m):
        l, r, c = map(int, input().split())
        l, r = l - 1, r - 1
        to[l].append([r, c])
    costs = [-1] * n
    hp = []
    heappush(hp, [0, 0])
    while hp:
        # print(hp)
        c, u = heappop(hp)
        if u == n - 1:
            print(c)
            # print(costs)
            exit()
        if costs[u] != -1: continue
        costs[u] = c
        for v, dc in to[u]:
            if costs[v] != -1: continue
            heappush(hp, [c + dc, v])
        for v in range(u - 1, 0, -1):
            if costs[v] != -1: break
            heappush(hp, [c, v])
    print(-1)

main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import defaultdict
from heapq import *

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    to = defaultdict(list)
    n, m = map(int, input().split())
    for _ in range(m):
        l, r, c = map(int, input().split())
        l, r = l - 1, r - 1
        to[l].append([r, c])
    costs = [-1] * n
    hp = []
    heappush(hp, [0, 0])
    while hp:
        #print(hp)
        c, u = heappop(hp)
        if u == n - 1:
            print(c)
            #print(costs)
            exit()
        if costs[u] != -1: continue
        costs[u] = c
        for v, dc in to[u]:
            if costs[v] != -1: continue
            heappush(hp, [c + dc, v])
            for w in range(v - 1, u, -1):
                if costs[w] != -1: break
                heappush(hp, [c + dc, w])

    print(-1)

main()


Fixed submission:
import sys
from collections import defaultdict
from heapq import *

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    to = defaultdict(list)
    n, m = map(int, input().split())
    for _ in range(m):
        l, r, c = map(int, input().split())
        l, r = l - 1, r - 1
        to[l].append([r, c])
    costs = [-1] * n
    hp = []
    heappush(hp, [0, 0])
    while hp:
        # print(hp)
        c, u = heappop(hp)
        if u == n - 1:
            print(c)
            # print(costs)
            exit()
        if costs[u] != -1: continue
        costs[u] = c
        for v, dc in to[u]:
            if costs[v] != -1: continue
            heappush(hp, [c + dc, v])
        for v in range(u - 1, 0, -1):
            if costs[v] != -1: break
            heappush(hp, [c, v])
    print(-1)

main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import defaultdict
from heapq import *

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    to = defaultdict(list)
    n, m = map(int, input().split())
    for _ in range(m):
        l, r, c = map(int, input().split())
        l, r = l - 1, r - 1
        to[l].append([r, c])
    costs = [-1] * n
    hp = []
    heappush(hp, [0, 0])
    while hp:
        #print(hp)
        c, u = heappop(hp)
        if u == n - 1:
            print(c)
            #print(costs)
            exit()
        if costs[u] != -1: continue
        costs[u] = c
        for v, dc in to[u]:
            if costs[v] != -1: continue
            heappush(hp, [c + dc, v])
            for w in range(v - 1, u, -1):
                if costs[w] != -1: continue
                heappush(hp, [c + dc, w])

    print(-1)

main()


Fixed submission:
import sys
from collections import defaultdict
from heapq import *

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    to = defaultdict(list)
    n, m = map(int, input().split())
    for _ in range(m):
        l, r, c = map(int, input().split())
        l, r = l - 1, r - 1
        to[l].append([r, c])
    costs = [-1] * n
    hp = []
    heappush(hp, [0, 0])
    while hp:
        # print(hp)
        c, u = heappop(hp)
        if u == n - 1:
            print(c)
            # print(costs)
            exit()
        if costs[u] != -1: continue
        costs[u] = c
        for v, dc in to[u]:
            if costs[v] != -1: continue
            heappush(hp, [c + dc, v])
        for v in range(u - 1, 0, -1):
            if costs[v] != -1: break
            heappush(hp, [c, v])
    print(-1)

main()