num = 1
prefix = 0


while num != 0:
    num = int(input())
    if num == 0:
        break
    str = input()
    num2= int(input())
    str2= input()
    if len(str) >= len(str2):
        for i in range(len(str2)):
            if str[i] == str2[i]:
                prefix+=1
            else:
                break
    else:
        for i in range(len(str)):
            if str2[i] == str[i]:
                prefix+=1
            else:
                break
    print(prefix)
    prefix = 0