n = input() l = len(n) if l < 3: print(0) exit() ans = 0 Stack = [] for i in range(l - 2): for j in range(i + 1, l - 1): for k in range(j + 1, l): if n[i] != '0' and n[i] != n[j] and n[j] == n[k]: Stack.append((int(n[i] + n[j] + n[k]), n[:i] + n[i + 1:j] + n[j + 1:k] + n[k + 1:])) if len(Stack) == 0: print(0) exit() preStack = Stack while preStack: Stack = [] for res, nn in preStack: ans = max(ans, res) l = len(nn) for i in range(l - 2): for j in range(i + 1, l - 1): for k in range(j + 1, l): if nn[i] != '0' and nn[i] != nn[j] and nn[j] == nn[k]: Stack.append((res + int(nn[i] + nn[j] + nn[k]), nn[:i] + nn[i + 1:j] + nn[j + 1:k] + nn[k + 1:])) preStack = Stack print(ans)