Your actual solution code is quite fast, albeit flawed. It’s your I/O code that is slowing you down. Here’s the same solution with faster I/O:
One thing I’ve done is use a BufferedReader instead of a Scanner to read input. But the most important thing on this particular problem is to perform I/O a line at a time. Reading in a million character line is pretty fast, even with Scanner, and it is not difficult to get the digits out of it. Printing is also much faster if you create a char array that holds the answer and print that, instead of printing one character at a time.
The flaw I’ve noticed with your code is that it doesn’t print anything when no solution exists. Not -1, it prints nothing at all, not even a newline.