This page is still under construction.

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

Painting the Balls

Time limit2sMemory limit512 MB

Summary
Given M range-painting operations used in order with unknown colors, count how many distinct final black/white ball colorings are possible.
Level

Medium7 of 10

Topics
Dynamic programming, Bit manipulation, Combinatorics
Solved
No attempts yet

Problem

NN balls sit in a row. A ball can be painted black or white, and every ball is white at the start. The leftmost ball is ball 1, and the balls are numbered in order from left to right.

Today you want to paint the balls. A machine does the painting, and it reads two integers LL and RR. The machine paints ball LL through ball RR entirely in one color, either white or black.

You used the machine MM times in total, and you know every LL and RR you gave it. You forgot which color each use painted.

Write a program that counts how many color combinations can appear after all MM uses of the machine.

Input

The first line contains the number of balls NN and the number of machine uses MM. (1≤N≤10001 \le N \le 1000, 1≤M≤501 \le M \le 50)

Each of the next MM lines contains the LL and RR given to the machine, in the order the machine was used. (1≤L≤R≤N1 \le L \le R \le N)

Output

Print the number of color combinations that can appear. The answer is at most 263−12^{63}-1.

Examples3

  1. Example 1

    Input
    4 3
    1 1
    2 2
    3 3
    
    Expected output
    8
    
  2. Example 2

    Input
    3 3
    1 3
    1 1
    2 3
    
    Expected output
    4
    
  3. Example 3

    Input
    1000 1
    47 747
    
    Expected output
    2