Compensation

Given scheduled trains with known delays, find the smallest start time of a booking whose promised arrival is at least 1800 seconds before any possible real arrival at station N.

Hard8Binary searchDynamic programmingGraphShortest pathNo attempts yetTime limit2sMemory limit512 MB

Problem

Train companies must pay compensation whenever a passenger reaches the destination at least 30 minutes (18001800 seconds) later than the booked itinerary promised. You want that money.

Your trip visits stations 1,2,,N1, 2, \dots, N in that order. Every scheduled train runs one leg, from some station XX to station X+1X + 1. A train has a planned departure time SS and a planned arrival time TT, and it is delayed by LL seconds on the day of travel, so it really departs at S+LS + L and really arrives at T+LT + L. You hold the timetable and the full list of delays, so you know how the day will go before you book anything.

A booking is one train for each of the N1N - 1 legs, chosen so that for every pair of consecutive legs the planned departure time of the later train is at least the planned arrival time of the earlier one. Changing trains takes no time. The start time of a booking is the planned departure time of its first train, and its promised arrival time is the planned arrival time of its last train.

On the day of travel you reach station 1 at the start time of your booking. From then on you may board any train you can physically catch: at station 1 a train whose real departure time is at least your start time, and at any later station a train whose real departure time is at least the real time you arrived there. You are not tied to the trains you booked.

A booking with start time ss and promised arrival time AA earns compensation when every travel plan that leaves station 1 at time ss either fails to reach station NN at all, or reaches it at a real time of at least A+1800A + 1800.

Times are plain second counts and never wrap around, so a delay can push a real arrival past 8640086400.

Find the smallest start time among the bookings that earn compensation.

Input

The first line contains two integers NN and MM (2N1002 \le N \le 100, 1M1051 \le M \le 10^5), the number of stations and the number of scheduled trains.

Each of the next MM lines describes one train with four integers XX, SS, TT and LL (1XN11 \le X \le N - 1, 0ST<864000 \le S \le T < 86400, 0L<864000 \le L < 86400): the train runs from station XX to station X+1X + 1, its planned departure time is SS, its planned arrival time is TT, and it is delayed by LL seconds.

Output

Print one line holding the start time of the earliest booking that earns compensation. If no booking earns it, print impossible instead.