#P2537. Minimum Magnetic Boundary Crossings

    ID: 15807 Type: Default 1000ms 256MiB

Minimum Magnetic Boundary Crossings

Minimum Magnetic Boundary Crossings

An exploration robot on Samuel Planet is on a mission to collect a peculiar mineral. The robot is trapped within a mysterious magnetic field region. Scientists have mapped out the region, which contains N magnetic fields. Each field is represented by a square whose sides are parallel to the coordinate axes. Note that the squares may be of different sizes and can intersect or even cover each other, but none of their edges or vertices coincide.

Initially, both the robot and the mineral are not located on any magnetic boundary. Due to technical limitations, the robot is restricted to horizontal and vertical moves only, and it is not allowed to move exactly along any magnetic field edge. However, the robot’s magnetic shield allows it to traverse the interior of these fields freely.

The goal is to design a route for the robot so that it collects the mineral by crossing as few magnetic field boundaries as possible. The minimum number of magnetic field boundary crossings required is defined as the number of squares for which one of the points (the robot's starting point or the mineral's location) lies strictly inside and the other lies strictly outside. A point (x, y) is considered strictly inside a square with bottom‐left coordinate (a, b) and side length l if and only if a < x < a+l and b < y < b+l.

For example, consider a case where there are 3 magnetic fields and the robot and the mineral are located such that the robot is inside one square while the mineral is outside, and vice versa for another square. In this scenario, the minimum number of magnetic field boundary crossings is 2.

Your task is to compute this minimum number given the positions of the robot, the mineral, and the details of the magnetic fields.

inputFormat

The input begins with an integer N (N ≥ 1), representing the number of magnetic fields.

The second line contains two integers xr and yr, representing the coordinates of the robot's starting position.

The third line contains two integers xm and ym, representing the coordinates of the mineral.

Each of the following N lines contains three integers a, b, and l. Here, (a, b) is the bottom‐left corner of a square magnetic field and l is its side length. It is guaranteed that the robot and the mineral do not lie on any magnetic field boundary.

outputFormat

Output a single integer denoting the minimum number of magnetic field boundaries that the robot must cross in order to reach the mineral.

sample

3
5 5
12 12
4 4 5
10 10 3
0 0 2
2