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

#GL0000131. BCNN trên bảng (LCM on Whiteboard)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

BCNN trên bảng (LCM on Whiteboard)

LCM on Whiteboard

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn positive integers, each given by prime factorization ai=∏j=1mipi,jei,ja_i=\prod_{j=1}^{m_i}p_{i,j}^{e_{i,j}}, with strictly increasing primes within each number. Choose exactly one position and replace its number with 1. Count the distinct LCM values obtainable from all possible chosen positions.

Input

The first line contains nn. For each i=1,…,ni=1,\ldots,n, read mim_i, followed by mim_i lines containing pairs pi,j,ei,jp_{i,j},e_{i,j}.

Output

Print the number of distinct LCMs obtainable after one replacement.

Subtasks

  • Subtask 1 (20 points): 1≤n≤101\le n\le 10; 1≤mi1\le m_i; ∑imi≤30\sum_i m_i\le 30; 2≤pi,j≤1 0002\le p_{i,j}\le 1\,000 (nguyên tố, tăng nghiêm ngặt trong mỗi số); 1≤ei,j≤1 0001\le e_{i,j}\le 1\,000.
  • Subtask 2 (30 points): 1≤n≤2 0001\le n\le 2\,000; 1≤mi1\le m_i; ∑imi≤5 000\sum_i m_i\le 5\,000; 2≤pi,j≤1 000 0002\le p_{i,j}\le 1\,000\,000 (nguyên tố, tăng nghiêm ngặt trong mỗi số); 1≤ei,j≤1 000 0001\le e_{i,j}\le 1\,000\,000.
  • Subtask 3 (50 points): 1≤n≤200 0001\le n\le 200\,000; 1≤mi1\le m_i; ∑imi≤200 000\sum_i m_i\le 200\,000; 2≤pi,j≤1 000 000 0002\le p_{i,j}\le 1\,000\,000\,000 (nguyên tố, tăng nghiêm ngặt trong mỗi số); 1≤ei,j≤1 000 000 0001\le e_{i,j}\le 1\,000\,000\,000.

Examples

Input

2
1
2 2
1
3 1

Output

2

Explanation

Replacing 222^2 with 1 leaves LCM 3; replacing 3 with 1 leaves LCM 4. These are distinct, hence the answer is 2.