proc getchar_unlocked():char {. importc:"getchar_unlocked",header: "" .} template `max=`*(x,y) = x = max(x,y) template `min=`*(x,y) = x = min(x,y) var X : array[-200010..200010,int] var xi = 0 var ans = 0 for i in 1..<4e6.int: let k = getchar_unlocked() if k < 'A' : break if k == 'A' : if X[xi] == 0 : X[xi] = i ans .max= i - X[xi] xi += 1 else: if X[xi] == 0 : X[xi] = i ans .max= i - X[xi] xi -= 1 echo ans