import java.util.Scanner;

public class Prefixo {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		
		while(in.hasNextInt()) {
			int N1 = in.nextInt();
			if(N1==0) break;
			
			String P = in.next().trim();
			
			int N2 = in.nextInt();
			
			String S = in.next().trim();
			
			int Q=0;
			
			for(int i=0;i<Math.min(N1, N2);i++) {
				if(P.charAt(i) == S.charAt(i)) {
					Q++;
				}else {
					break;
				}
			}
			
			System.out.println(Q);
		}
	}

}
