import java.util.Scanner object Main { def max(a: Int, b: Int): Int = { if (a < b) b else a } def min(a: Int, b: Int): Int = { if (a < b) a else b } def solve(sc: => Scanner): Unit = { val s = sc.next val (_, _, maxcnt, _) = (s + ' ').foldLeft((0, 0, 0, ' '))((acc, c) => (acc, c) match { case ((acnt, bcnt, maxcnt, prev), c) if prev != c => (if (c == 'A') 1 else acnt, if (c == 'B') 1 else bcnt, max(maxcnt, min(acnt, bcnt)), c) case ((acnt, bcnt, maxcnt, prev), 'A') => (acnt + 1, bcnt, maxcnt, 'A') case ((acnt, bcnt, maxcnt, prev), 'B') => (acnt, bcnt + 1, maxcnt, 'B') }) println(maxcnt*2) } def main(args: Array[String]): Unit = { val sc: Scanner = new Scanner(System.in) solve(sc) } }