def cw(time, period, start):
    if time<start:
        return start - time
    now = time - start
    sw = (now//period)%2
    return sw * (period - now % period)

temp = list(map(int,input().split()))

n = temp[0]
k = temp[1]
cross = []
for i in range(k):
    x = list(map(int,input().split()))
    cross.append(x)
cross.sort(key=lambda x:x[0])
mytime = 0
mypos = 0
x = 0
for x,t,s in cross:
    mytime += x-mypos
    mytime += cw(mytime,t,s)
    mypos = x
mytime = mytime + n-mypos
print(mytime)

'Python > 코딩 테스트' 카테고리의 다른 글

백준 28114 python  (0) 2024.08.13
백준 2941 python  (0) 2024.08.13
백준 1316 python  (0) 2024.08.13
백준 10828 python  (0) 2024.08.13
백준 1193 python  (0) 2024.08.13