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

#GL0000158. Bài toán GCD đơn giản (bản dễ) (A Simple GCD Problem (Easy Version))

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Bài toán GCD đơn giản (bản dễ) (A Simple GCD Problem (Easy Version))

A Simple GCD Problem (Easy Version)

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given arrays a1,…,ana_1,\ldots,a_n and b1,…,bnb_1,\ldots,b_n of positive integers. At each position ii, you may operate at most once: replace aia_i by an integer m≠aim\ne a_i satisfying 1≤m≤bi1\le m\le b_i. In this easy version, bi=aib_i=a_i at every index.

Let a′a' be the resulting array. For every contiguous subarray of length at least two, its GCD must stay equal to the GCD of the corresponding original subarray:

$$\gcd(a_l,\ldots,a_r)=\gcd(a'_l,\ldots,a'_r),\qquad 1\le l<r\le n.$$

Find the maximum number of positions you can change. Positions with no operation retain their original values.

Input

The first line contains nn. The second contains the values of aa, and the third contains those of bb.

Output

Print the maximum number of performed operations.

Subtasks

  • Subtask 1 (20 points): 2≤n≤152 \le n \le 15; 1≤ai=bi≤501 \le a_i = b_i \le 50.
  • Subtask 2 (30 points): 2≤n≤40002 \le n \le 4000; 1≤ai=bi≤1061 \le a_i = b_i \le 10^6.
  • Subtask 3 (50 points): 2≤n≤2⋅1052 \le n \le 2\cdot10^5; 1≤ai=bi≤1091 \le a_i = b_i \le 10^9.

Examples

Input

4
1 2 3 4
1 2 3 4

Output

3

Explanation

All adjacent GCDs in [1,2,3,4][1,2,3,4] are 11, so every subarray of length at least two has GCD 11. Replacing the last three elements by 11 yields [1,1,1,1][1,1,1,1] and preserves the GCD of every such subarray. The first element cannot be reduced below 11, so three operations are optimal.