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

#GL0000142. Chào buổi sáng, ƯCLN (Ya Sabah El GCD)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Chào buổi sáng, ƯCLN (Ya Sabah El GCD)

Ya Sabah El GCD

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given two arrays aa and bb, both of length nn, with indices from 11 to nn.

You may choose an index ii and replace aia_i with gcd⁡(ai,bi)\gcd(a_i,b_i), paying a cost of i2+ii^2+i. Repeating the same index has no further effect.

Find the minimum total cost needed to make gcd⁡(a1,a2,…,an)=1\gcd(a_1,a_2,\ldots,a_n)=1. Print -1 if this is impossible.

Input

The first line contains nn; the second contains aia_i; the third contains bib_i.

Output

Print the minimum total cost, or -1 if achieving GCD 11 is impossible.

Subtasks

  • Subtask 1 (20 points): 1≤n≤121\le n\le 12; 1≤ai,bi≤2001\le a_i,b_i\le 200.
  • Subtask 2 (30 points): 1≤n≤5001\le n\le 500; 1≤ai,bi≤1 000 0001\le a_i,b_i\le 1\,000\,000.
  • Subtask 3 (50 points): 1≤n≤5 0001\le n\le 5\,000; 1≤ai,bi≤1 000 000 000 0001\le a_i,b_i\le 1\,000\,000\,000\,000.

Examples

Input

3
6 30 60
5 10 21

Output

2

Explanation

Initially gcd⁡(6,30,60)=6\gcd(6,30,60)=6. Applying the operation at index 11 costs 12+1=21^2+1=2 and replaces 66 with gcd⁡(6,5)=1\gcd(6,5)=1. The resulting array has GCD 11. Since every operation costs at least 22, the optimum is 22.