Unique pairs of numbers in collections

Java Medium collections

Task «Unique pairs of numbers in collections»

You are given two lists of integers. You need to find all unique pairs (a, b), where a belongs to the first list, b belongs to the second list, and the sum a + b is a prime number. The uniqueness of a pair is determined by the values of a and b (order does not matter: (a, b) and (b, a) are considered different if a != b). Output the number of such pairs and the pairs themselves in ascending order of a, and for equal a, in ascending order of b.

Input Format

The first line contains two integers N and M (1 ≤ N, M ≤ 1000) — the sizes of the first and second lists. The second line contains N integers — the elements of the first list. The third line contains M integers — the elements of the second list. All numbers do not exceed 10^6 in absolute value.

Output Format

In the first line, output a single number K — the number of unique pairs. In the next K lines, output the pairs themselves in the format "a b", each on a new line, in ascending order of a, and for equal a, in ascending order of b.

Examples

Example 1

INPUT
3 3 1 2 3 4 5 6
OUTPUT
4 1 4 1 6 2 5 3 4

Example 2

INPUT
2 2 10 20 30 40
OUTPUT
0

Example 3

INPUT
My IP is 192.168.1.1 and 10.0.0.255
OUTPUT
192.168.1.1 10.0.0.255
main.java