Simple, but definitely non-optimal.

how to do it with less walking in the worst case?