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

#GL0000159. Bài toán GCD đơn giản (bản khó) (A Simple GCD Problem (Hard 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 khó) (A Simple GCD Problem (Hard Version))

A Simple GCD Problem (Hard Version)

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given positive-integer arrays aa and bb, each of length nn. At most once per position ii, you may replace aia_i by a different positive integer mm with 1≤m≤bi1\le m\le b_i. Here bib_i is independent of aia_i and may be smaller or larger.

After all operations, the GCD of every contiguous subarray of length at least two must be exactly the same as before. Formally, for all 1≤l<r≤n1\le l<r\le n,

gcd⁡(al,…,ar)=gcd⁡(al′,…,ar′).\gcd(a_l,\ldots,a_r)=\gcd(a'_l,\ldots,a'_r).

Maximize the number of changed positions. If you do not change index ii, its original value is allowed even when ai>bia_i>b_i.

Input

The first line contains nn, followed by a line with aia_i and a line with bib_i.

Output

Print the maximum number of positions that can be changed.

Subtasks

  • Subtask 1 (20 points): 2≤n≤72 \le n \le 7; 1≤ai,bi≤501 \le a_i,b_i \le 50.
  • Subtask 2 (30 points): 2≤n≤10002 \le n \le 1000; 1≤ai,bi≤1061 \le a_i,b_i \le 10^6.
  • Subtask 3 (50 points): 2≤n≤5⋅1042 \le n \le 5\cdot10^4; 1≤ai,bi≤1091 \le a_i,b_i \le 10^9.

Examples

Input

3
2 2 2
4 4 4

Output

2

Explanation

Initially every adjacent GCD and the whole-array GCD equal 22. Replacing the first and third values by 44 yields [4,2,4][4,2,4], which preserves them all and makes two changes. Changing all three values is impossible while maintaining both adjacent GCDs exactly equal to 22.