import re

p = re.compile('[a-zA-Z]+')

Inputs = [list() for _ in range(5)]
for ctr in range(10):
    Inputs[ctr // 2].append(input())


def dpTableBuilder(String1, String2):
    dpTable = [[0] * (len(String2) + 1) for i in range(len(String1) + 1)]
    for char1 in range(len(String1)):
        for char2 in range(len(String2)):
            if String1[char1] == String2[char2]: dpTable[char1 + 1][char2 + 1] = dpTable[char1][char2] + 1
            else: dpTable[char1 + 1][char2 + 1] = 0
    return dpTable


def longestCommonStringFinder(String1, String2):
    '''
    :param String1: String 1 that needs to compare
    :param String2: String 2 that needs to compare
    :return: a list() that contains tuple(start, end), and String1[start:end] will be the common String
    '''

    dpTable = dpTableBuilder(String1, String2)
    maxLength = int()
    CommonStrings = list()
    for check_y in range(len(dpTable)):
        for check_x in range(len(dpTable[0])):
            if maxLength < dpTable[check_y][check_x]:
                maxLength = dpTable[check_y][check_x]
                CommonStrings = list()
                CommonStrings.append((check_y - maxLength, check_y, check_x - maxLength, check_x))
            elif maxLength == dpTable[check_y][check_x]:
                CommonStrings.append((check_y - maxLength, check_y, check_x - maxLength, check_x))
    return CommonStrings


def validCommonStringFinder(String1, String2):
    '''
    :param String1: String1 that will be used to compare here
    :param String2: String2 that will be used to compare here
    :return:    Two tuples, each tuple will be (str, int, int)
                str is the common string that will be deleted
                int1 is the start of common string
                int2 is the end of common string
    '''
    commonStrings = longestCommonStringFinder(String1, String2)
    commonStrings1 = list()
    commonStrings2 = list()
    for common in commonStrings:
        commonStrings1.append((String1[common[0]:common[1]], common[0], common[1]))
        commonStrings2.append((String2[common[2]:common[3]], common[2], common[3]))
    commonStrings1.sort(key=lambda item: (item[0], item[1]))
    commonStrings2.sort(key=lambda item: (item[0], item[1]))
    return commonStrings1[0], commonStrings2[0]


def stringSplit(String1, String2, info1, info2):
    '''
    info1, info2 = validCommonStringFinder(String1, String2)
    '''
    return (String1[0:info1[1]], String2[0:info2[1]]), (String1[info1[2]:len(String1)], String2[info2[2]:len(String2)])


def ADFCalculate(String1, String2):
    """
    Calculate the ADF Value of Strings
    :param String1: String after regex
    :param String2: String after regex
    :return: ADF Value

    TEST_CASE_0 - Sample Case
    >>> ADFCalculate('IAMGOINGHOMENOW', 'IWILLGOHOMESOON')
    8
    >>> ADFCalculate('THEBIGBLACKBEARBITABIGBLACKDOG', 'THEBIGBLACKBUGBLEDBLACKBLOOD')
    19
    >>> ADFCalculate('IAMGOINGHOMENOW', 'IWILLGOHOMENOW')
    10
    >>> ADFCalculate('COMPLEMENTARYANGLEMEASURESSUMTODEGREES', 'THEMEASUREOFSUPPLEMENTARYANGLESADDTODEGREES')
    26
    >>> ADFCalculate('ATALEOFTWOCITIESWASPUBLISHEDBYDICKENSIN', 'INCHARLESDICKENSPUBLISHEDNICHOLASNICKLEBY')
    18
    >>> ADFCalculate('CONNECTICUTISTHECONSTITUTIONSTATE', 'HARTFORDISTHECAPITOLOFCONNECTICUT')
    11



    TEST_CASE_1 - Empty / No ADF
    >>> ADFCalculate('A', 'B')
    0
    >>> ADFCalculate('', '')
    0

    TEST_CASE_2 - Time Complexity Test
    >>> ADFCalculate('WHICHOFTHETWOLOOPSABOVETAKESLESSCODEWHILEYOUCANWRITEANYLOOPWITHEITHERAWHILEORFORPROGRAMMERSTENDTOUSETHEWHILEWHENTHEYDONTKNOWHOWMANYTIMESTHELOOPWILLEXECUTEANDTHEFORWHENTHEYKNOWTHENUMBEROFTIMESTOEXECUTETHELOOPTHEPROBLEMWITHUSINGAWHILELOOPTOEXECUTEALOOPACERTAINNUMBEROFTIMESISTHATYOUHAVETOREMEMBERTOUPDATETHEVARIABLEINTHELOOPTHEFORLOOPALLOWSYOUTOSPECIFYALLOFTHEIMPORTANTTHINGSABOUTALOOPINONEPLACEWHATVALUEDOVARIABLESSTARTATWHATISTHECONDITIONTOTESTANDHOWTHELOOPVARIABLESCHANGE','ANOTEONTERMINOLOGYWHENISAYTENSORINTHISTUTORIALITREFERSTOANYTORCHTENSOROBJECTMATRICESANDVECTORSARESPECIALCASESOFTORCHTENSORSWHERETHEIRDIMENSIONISANDRESPECTIVELYWHENIAMTALKINGABOUTDTENSORSIWILLEXPLICITLYUSETHETERMDTENSOR')
    41

    TEST_CASE_3 - Correctness Test
    >>> ADFCalculate('AFAFAFA','FAFAFAF')
    6
    >>> ADFCalculate('ABCDABCD', 'DCBADCBA')
    3
    """
    ADF = int()
    if len(String1) == 0 or len(String2) == 0: return 0
    else:
        info1, info2 = validCommonStringFinder(String1, String2)
        if len(info1[0]) == 0: return 0
        ADF += len(info1[0])
        SplitString1, SplitString2 = stringSplit(String1, String2, info1, info2)
        ADF += ADFCalculate(SplitString1[0], SplitString1[1])
        ADF += ADFCalculate(SplitString2[0], SplitString2[1])
        return ADF


for main in range(5):
    String1, String2 = str(), str()
    for char1 in p.findall(Inputs[main][0]): String1 += char1.upper()
    for char2 in p.findall(Inputs[main][1]): String2 += char2.upper()
    print(ADFCalculate(String1, String2))