This page is still under construction.

Parts of this page are still being built. What you see may change.

Guessing Game

Time limit1sMemory limit128 MB

Summary
Find the largest prefix of interval parity answers that stays consistent with some 0/1 sequence of length one billion.
Level

Medium7 of 10

Topics
Union-find, Prefix sum, Hash map
Solved
No attempts yet

Problem

Byteman and Bitman play the following game. Bitman secretly writes down a sequence of 1,000,000,0001{,}000{,}000{,}000 zeros and ones. Byteman's goal is to guess this sequence.

Byteman repeatedly asks Bitman questions of the form:

Is the sum of the subsequence that begins at the bb-th element and ends at the ee-th element of your sequence even or odd?

After playing for a while, Byteman began to suspect that Bitman was answering dishonestly. He would like to know how many of his questions, counting from the first, can still be answered consistently.

Write a program that computes the greatest number mm for which there exists a sequence of zeros and ones consistent with Bitman's answers to the first mm questions.

Input

The first line contains one integer nn (0≤n≤100,0000 \le n \le 100{,}000), the number of Byteman's questions.

Each of the following nn lines describes one question together with Bitman's answer as three integers bb, ee, and ss (1≤b≤e≤1,000,000,0001 \le b \le e \le 1{,}000{,}000{,}000, s∈{0,1}s \in \{0, 1\}) separated by single spaces. Here bb and ee are the positions of the first and last elements of the subsequence in that question. s=0s = 0 means the answer was that the sum is even, and s=1s = 1 means it is odd.

Output

Output a single integer mm: the greatest value such that a sequence of zeros and ones consistent with the first mm answers exists.

Examples1

  1. Example 1

    Input
    5
    3 3 0
    2 5 1
    1 4 0
    2 5 0
    1 5 1
    
    Expected output
    3