One day Nikita found the string containing letters "a" and "b" only. 

Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b".

Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get?
The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b".
It the first sample the string is already beautiful.

In the second sample he needs to delete one of "b" to make it beautiful.
abba
4
bab
2
bbabbbaabbbb
9
bbabbbbbaaba
10
bbabbbababaa
9
aabbaababbab
8
a
1
b
1
ab
2
ba
2
bb
2
aa
2
babbbaab
6
abaaaa
6
aaa
3
# 887B
cubes_sets = [set(), set(), set()]

def myor(smlist):
        buff_list = [0, 0, 0]
        for x in smlist:
                for y in range(3):
                        buff_list[y] = max(x[y], buff_list[y])
        # print(smlist, buff_list)
        return buff_list


def mycount(strnum):
    global cubes_sets
    buff_cubes = cubes_sets
    buffl = [[0, 0, 0]] * len(strnum)
    for x, y in enumerate(list(strnum)):
            # print(y, buff_cubes)
            if y in buff_cubes[0]:
                buffl[x][0] = 1
            if y in buff_cubes[1]:
                buffl[x][1] = 1
            if y in buff_cubes[2]:
                buffl[x][2] = 1
            if not (y in buff_cubes[2] or y in buff_cubes[1] or y in buff_cubes[0]):
                return [[0, 0, 0]] * len(strnum)
    return buffl


def can_make(num):
        buff_num = str(num)
        global cubes_sets
        buff_cubes = cubes_sets
        if len(buff_num) == 1:
                if buff_num in cubes_sets[0] or buff_num in cubes_sets[1] or buff_num in cubes_sets[2]:
                        return True
        elif len(buff_num) == 2:
                inn = mycount(buff_num)
                if sum(myor(inn)) >= 2:
                    return True
        elif len(buff_num) == 3:
                inn = inn = mycount(buff_num)
                if sum(myor(inn)) == 3:
                    return True
                return False

def main():
    global cubes_sets
    n = int(input())
    for x in range(n):
        cubes_sets[x] = set(input().split())
    lastn = 0
    for x in range(1, 1000):
        if can_make(x):
            lastn = x
            continue
        else:
            return lastn



if __name__ == "__main__":
    print(main())
