Buggy python submission (verdict: Time Limit Exceeded):
def sarch(listd, num, i):
    if listd[i - 2] == num:
        return 1
    return 0

N, M = map(int, input().split())
data = []
brige = []
for i in range(M):
    A, B = map(int, input().split())
    data.append(B)
    listdata = [0 for j in range(1, A)]
    for j in range(A, B):
        listdata.append(j)
    listdata.extend([0 for j in range(B, N)])
    brige.append(listdata)


ans = []


keylist = [0 for i in range(M)]

for i in range(1, N + 1):
    for j in range(M):
        if keylist[j] != 1 and sarch(brige[j], i, data[j]) == 1:
            ans.append(i)
            for k in range(M):
                if brige[k][i - 1] == i:
                    keylist[k] = 1
            keylist[j] = 1


print(len(ans))


Fixed submission:
N, M = map(int, input().split())
AB = []
for i in range(M):
    a, b = map(int, input().split())
    AB.append([b, a])
AB.sort()
for i in range(M):
    if i == 0:
       j = AB[i][0] - 1
       ans = 1
    else:
        if AB[i][1] <= j and j < AB[i][0]:
            1
        else:
            j = AB[i][0] - 1
            ans += 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def sarch(listd, num):
    if listd[len(listd) - 1] == num:
        for i in listd:
            for j in ans:
                if i == j:
                    return 0
        return 1
    return 0

N, M = map(int, input().split())
data = []
brige = []
for i in range(M):
    A, B = map(int, input().split())
    data.append([A, B])
    listdata = []
    for j in range(A, B):
        listdata.append(j)
    brige.append(listdata)


ans = []

keylist = [0 for i in range(M)]

for i in range(1, N + 1):
    for j in range(M):
        if keylist[j] != 1 and sarch(brige[j], i) == 1:
            ans.append(i)
            keylist[j] = 1
    

print(len(ans))


Fixed submission:
N, M = map(int, input().split())
AB = []
for i in range(M):
    a, b = map(int, input().split())
    AB.append([b, a])
AB.sort()
for i in range(M):
    if i == 0:
       j = AB[i][0] - 1
       ans = 1
    else:
        if AB[i][1] <= j and j < AB[i][0]:
            1
        else:
            j = AB[i][0] - 1
            ans += 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def sarch(listd, num):
    if listd[len(listd) - 1] == num:
        return 1
    return 0

N, M = map(int, input().split())
data = []
brige = []
for i in range(M):
    A, B = map(int, input().split())
    data.append([A, B])
    listdata = []
    for j in range(A, B):
        listdata.append(j)
    brige.append(listdata)


ans = []

keylist = [0 for i in range(M)]

for i in range(1, N + 1):
    for j in range(M):
        if keylist[j] != 1 and sarch(brige[j], i) == 1:
            ans.append(i)
            keylist[j] = 1
            for k in range(M):
                for l in brige[k]:
                    if l == i:
                        keylist[k] = 1
                        break
    

print(len(ans))


Fixed submission:
N, M = map(int, input().split())
AB = []
for i in range(M):
    a, b = map(int, input().split())
    AB.append([b, a])
AB.sort()
for i in range(M):
    if i == 0:
       j = AB[i][0] - 1
       ans = 1
    else:
        if AB[i][1] <= j and j < AB[i][0]:
            1
        else:
            j = AB[i][0] - 1
            ans += 1

print(ans)