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

#GL0000132. Chia cho 2 hoặc 3 (Divide by 2 or 3)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Chia cho 2 hoặc 3 (Divide by 2 or 3)

Divide by 2 or 3

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

Given nn positive integers, each operation divides a chosen array element by 2 if divisible by 2, or by 3 if divisible by 3. Determine the minimum number of operations required to make all elements equal, or print -1 if impossible.

Input

The first line contains nn; the second contains nn integers aia_i.

Output

Print the minimum number of operations or -1 if impossible.

Subtasks

  • Subtask 1 (20 points): 2≤n≤202\le n\le 20; 1≤ai≤1 0001\le a_i\le 1\,000.
  • Subtask 2 (30 points): 2≤n≤1502\le n\le 150; 1≤ai≤1 000 0001\le a_i\le 1\,000\,000.
  • Subtask 3 (50 points): 2≤n≤1 0002\le n\le 1\,000; 1≤ai≤1 000 000 0001\le a_i\le 1\,000\,000\,000.

Examples

Input

3
1 4 3

Output

3

Explanation

Changing 4 to 1 takes two divisions by 2; changing 3 to 1 takes one division by 3. The array becomes [1,1,1] in three necessary operations.