Problem: There are many blue cards and red cards on the table. For each card, an integer number greater than 1 is printed on its face. The same number may be printed on several cards.

A blue card and a red card can be paired when both of the numbers printed on them have a common divisor greater than 1. There may be more than one red card that can be paired with one blue card. Also, there may be more than one blue card that can be paired with one red card. When a blue card and a red card are chosen and paired, these two cards are removed from the whole cards on the table.

<image>
Figure E-1: Four blue cards and three red cards

For example, in Figure E-1, there are four blue cards and three red cards. Numbers 2, 6, 6 and 15 are printed on the faces of the four blue cards, and 2, 3 and 35 are printed on those of the three red cards. Here, you can make pairs of blue cards and red cards as follows. First, the blue card with number 2 on it and the red card with 2 are paired and removed. Second, one of the two blue cards with 6 and the red card with 3 are paired and removed. Finally, the blue card with 15 and the red card with 35 are paired and removed. Thus the number of removed pairs is three.

Note that the total number of the pairs depends on the way of choosing cards to be paired. The blue card with 15 and the red card with 3 might be paired and removed at the beginning. In this case, there are only one more pair that can be removed and the total number of the removed pairs is two.

Your job is to find the largest number of pairs that can be removed from the given set of cards on the table.

Input

The input is a sequence of datasets. The number of the datasets is less than or equal to 100. Each dataset is formatted as follows.

> m n
>  b1 ... bk ... bm
>  r1 ... rk ... rn
>

The integers m and n are the number of blue cards and that of red cards, respectively. You may assume 1 ≤ m ≤ 500 and 1≤ n ≤ 500. bk (1 ≤ k ≤ m) and rk (1 ≤ k ≤ n) are numbers printed on the blue cards and the red cards respectively, that are integers greater than or equal to 2 and less than 10000000 (=107). The input integers are separated by a space or a newline. Each of bm and rn is followed by a newline. There are no other characters in the dataset.

The end of the input is indicated by a line containing two zeros separated by a space.

Output

For each dataset, output a line containing an integer that indicates the maximum of the number of the pairs.

Sample Input


4 3
2 6 6 15
2 3 5
2 3
4 9
8 16 32
4 2
4 9 11 13
5 7
5 5
2 3 5 1001 1001
7 11 13 30 30
10 10
2 3 5 7 9 11 13 15 17 29
4 6 10 14 18 22 26 30 34 38
20 20
195 144 903 63 137 513 44 626 75 473
876 421 568 519 755 840 374 368 570 872
363 650 155 265 64 26 426 391 15 421
373 984 564 54 823 477 565 866 879 638
100 100
195 144 903 63 137 513 44 626 75 473
876 421 568 519 755 840 374 368 570 872
363 650 155 265 64 26 426 391 15 421
373 984 564 54 823 477 565 866 879 638
117 755 835 683 52 369 302 424 513 870
75 874 299 228 140 361 30 342 750 819
761 123 804 325 952 405 578 517 49 457
932 941 988 767 624 41 912 702 241 426
351 92 300 648 318 216 785 347 556 535
166 318 434 746 419 386 928 996 680 975
231 390 916 220 933 319 37 846 797 54
272 924 145 348 350 239 563 135 362 119
446 305 213 879 51 631 43 755 405 499
509 412 887 203 408 821 298 443 445 96
274 715 796 417 839 147 654 402 280 17
298 725 98 287 382 923 694 201 679 99
699 188 288 364 389 694 185 464 138 406
558 188 897 354 603 737 277 35 139 556
826 213 59 922 499 217 846 193 416 525
69 115 489 355 256 654 49 439 118 961
0 0


Output for the Sample Input


3
1
0
4
9
18
85






Example

Input

4 3
2 6 6 15
2 3 5
2 3
4 9
8 16 32
4 2
4 9 11 13
5 7
5 5
2 3 5 1001 1001
7 11 13 30 30
10 10
2 3 5 7 9 11 13 15 17 29
4 6 10 14 18 22 26 30 34 38
20 20
195 144 903 63 137 513 44 626 75 473
876 421 568 519 755 840 374 368 570 872
363 650 155 265 64 26 426 391 15 421
373 984 564 54 823 477 565 866 879 638
100 100
195 144 903 63 137 513 44 626 75 473
876 421 568 519 755 840 374 368 570 872
363 650 155 265 64 26 426 391 15 421
373 984 564 54 823 477 565 866 879 638
117 755 835 683 52 369 302 424 513 870
75 874 299 228 140 361 30 342 750 819
761 123 804 325 952 405 578 517 49 457
932 941 988 767 624 41 912 702 241 426
351 92 300 648 318 216 785 347 556 535
166 318 434 746 419 386 928 996 680 975
231 390 916 220 933 319 37 846 797 54
272 924 145 348 350 239 563 135 362 119
446 305 213 879 51 631 43 755 405 499
509 412 887 203 408 821 298 443 445 96
274 715 796 417 839 147 654 402 280 17
298 725 98 287 382 923 694 201 679 99
699 188 288 364 389 694 185 464 138 406
558 188 897 354 603 737 277 35 139 556
826 213 59 922 499 217 846 193 416 525
69 115 489 355 256 654 49 439 118 961
0 0


Output

3
1
0
4
9
18
85

Solution (in JAVA):

Solution:


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class Main 
{
	static int INF = 999999999;
	static Left NIL = new Left(-1);
	static Left[] pairv;
	
	static boolean BFS()
	{
		LinkedList<Left> q = new LinkedList<>();
		for(Left u : l)
		{
			if(u.pair == -1)
			{
				u.dist = 0;
				q.add(u);
			}
			else u.dist = INF;
		}
		NIL.dist = INF;
		while(!q.isEmpty())
		{
			Left u = q.poll();
			if(u.dist < NIL.dist)
			{
				for(int v : u.adj)
				{
					if(pairv[v].dist == INF)
					{
						pairv[v].dist = u.dist+1;
						q.add(pairv[v]);
					}
				}
			}
		}
		return NIL.dist != INF;
	}
	
	static boolean DFS(Left u)
	{
		if(u != NIL)
		{
			for(int v : u.adj)
				if(pairv[v].dist == u.dist + 1)
					if(DFS(pairv[v]))
					{
						pairv[v] = u;
						u.pair = v;
						return true;
					}
			u.dist = INF;
			return false;
		}
		return true;
	}
	
	static class Left
	{
		HashSet<Integer> adj = new HashSet<>();
		int id;
		int pair = -1;
		int dist = -1;
		
		public Left(int i)
		{
			id=i;
		}
	}
	
	static class En
	{
		int a;
		int b;
		
		public En(int a0, int b0)
		{
			a0 = a;
			b0 = b;
		}
	}
	
	static class C implements Comparator<En>
	{

		@Override
		public int compare(En o1, En o2) 
		{
			return o1.a-o2.a;
		}
		
	}

	static Left[] l;
	
	public static void main(String[] args) 
	{
		int MAX = 10000000;
		int[] sieve = new int[MAX+1];
		sieve[0] = 1;
		sieve[1] = 1;
		for(long i = 2; i <= MAX; i++)
		if(sieve[(int)i] == 0)
		{
			sieve[(int)i] = (int)i;
			for(long j = i*i; j <= MAX; j+= i)
			{
				sieve[(int)j] = (int)i;
			}
		}
		
		StringBuilder sb = new StringBuilder();
		for(int i = 0; i < 100; i++)
		{
//			System.out.print(i + ":" + sieve[i]+ ". ");
		}
		
		long timea = 0;
		long timeb = 0;
		long timec = 0;
		long timed = 0;
		FastReader sc = new FastReader();
		
		int[] has = new int[MAX+1];
		int curhaser = 0;
		
		while(true)
		{
			long curtime = System.currentTimeMillis();
			int n = sc.nextInt();
			int m = sc.nextInt();
			if(n==0)break;
			int[] a = new int[n];
			int[] b = new int[m];
			l = new Left[n];
			
			
			for(int i = 0; i < n; i++)
			{
				l[i] = new Left(i);
			}
			
			for(int i = 0; i < n; i++)
				a[i] = sc.nextInt();
			for(int i = 0; i < m; i++)
				b[i] = sc.nextInt();


			timea += (System.currentTimeMillis() - curtime);
			curtime = System.currentTimeMillis();
			
			
			C c = new C();
			PriorityQueue<En> pq = new PriorityQueue<En>(c);
			
			for(int i = 0; i < MAX+1; i++)
				has[i] = -1;
			
			timeb += (System.currentTimeMillis() - curtime);
			curtime = System.currentTimeMillis();
			
			for(int j = 0; j < m; j++)
			{
				curhaser++;
				int cur = b[j];
				int orcur = cur;
				
				while(cur > 1)
				{
					has[sieve[cur]] = curhaser;
//					System.out.println(orcur + " has  " + sieve[cur]);
					cur /= sieve[cur];
				}
				
				for(int i = 0; i < n; i++)
				{
					int curi = a[i];
					while(curi > 1)
					{
//						System.out.println(i + " " + j + " " + curi);
						if(has[sieve[curi]] == curhaser)
						{ //found common divisor with this left-card
							pq.add(new En(i, j));
							l[i].adj.add(j);
//							System.out.println("Pairing " + a[i] + " " + orcur + " through " + sieve[curi]);
							break;
						}
						curi /= sieve[curi];
					}
				}
			}
//			System.out.println("Stage 1");
			timec += (System.currentTimeMillis() - curtime);
			curtime = System.currentTimeMillis();
			
			boolean[] pa = new boolean[n];
			boolean[] pb = new boolean[m];
			pairv = new Left[m];
			for(int i = 0; i < m; i++)
				pairv[i] = NIL;
			
			int matching = 0;
			int it = 0;
//			System.out.println("Go");
			while(BFS())
			{
				it++;
//				System.out.println("it " + it);
				for(int i = 0; i < n; i++)
				{
					Left u = l[i];
					if(u.pair == -1)
						if(DFS(u))
							matching++;
				}
			}
//			System.out.println("Stage 2");
			sb.append(matching + "\n");
			timed += (System.currentTimeMillis() - curtime);
			curtime = System.currentTimeMillis();
		}
		System.out.print(sb);
//		System.out.println(timea + " " + timeb + " " + timec + " " + timed);
	}
	
	
	
	
	
	
	
	
	
	
	
    static class FastReader 
    { 
        BufferedReader br; 
        StringTokenizer st; 
  
        public FastReader() 
        { 
            br = new BufferedReader(new
                     InputStreamReader(System.in)); 
        } 
  
        String next() 
        { 
            while (st == null || !st.hasMoreElements()) 
            { 
                try
                { 
                    st = new StringTokenizer(br.readLine()); 
                } 
                catch (IOException  e) 
                { 
                    e.printStackTrace(); 
                } 
            } 
            return st.nextToken(); 
        } 
  
        int nextInt() 
        { 
            return Integer.parseInt(next()); 
        } 
  
        long nextLong() 
        { 
            return Long.parseLong(next()); 
        } 
  
        double nextDouble() 
        { 
            return Double.parseDouble(next()); 
        } 
  
        String nextLine() 
        { 
            String str = ""; 
            try
            { 
                str = br.readLine(); 
            } 
            catch (IOException e) 
            { 
                e.printStackTrace(); 
            } 
            return str; 
        } 
    } 	

}