Working on TOJ - Problem 1877