PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#DSU0000091. Nối các điểm (Connect the Dots)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Nối các điểm (Connect the Dots)

Connect the Dots

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn points numbered from 11 to nn, initially with no connections. Each of the mm operations (a,d,k)(a,d,k) selects points a,a+d,…,a+kda,a+d,\ldots,a+kd and connects every pair of selected points. After all operations, determine the number of connected components in the resulting undirected graph. Operations may add redundant connections.

Input

The first line contains n,mn,m. Each of the next mm lines contains a,d,ka,d,k.

Output

Print the number of connected components.

Subtasks

  • Subtask 1 (20 points): 1≤n≤351\le n\le 35, 1≤m≤401\le m\le 40, 1≤d≤101\le d\le 10, 1≤a≤a+kd≤n1\le a\le a+kd\le n, 0≤k≤n0\le k\le n.
  • Subtask 2 (30 points): 1≤n≤3001\le n\le 300, 1≤m≤4001\le m\le 400, 1≤d≤101\le d\le 10, 1≤a≤a+kd≤n1\le a\le a+kd\le n, 0≤k≤n0\le k\le n.
  • Subtask 3 (50 points): 1≤n,m≤2⋅1051\le n,m\le 2\cdot10^5, 1≤d≤101\le d\le 10, 1≤a≤a+kd≤n1\le a\le a+kd\le n, 0≤k≤n0\le k\le n.

Examples

Input

10 2
1 2 4
2 2 4

Output

2

Explanation

Odd-indexed points form one component and even-indexed points another.