from collections import deque # import psyco; psyco.full() def sum_digits(n): tot = 0 while n: tot += n % 10 n //= 10 return tot def ant(): seen = set() count = 0 stack = deque([(1000, 1000)]) while stack: p = stack.pop() if p not in seen: seen.add(p) x, y = p if sum_digits(x) + sum_digits(y) <= 25: count += 1 stack.append((x+1, y )) stack.append((x-1, y )) stack.append((x, y+1)) stack.append((x, y-1)) return count print ant()