Answer
Here are the contents of the containers after each stage:
A B
0 7 fill B
3 4 fill A from B
0 4 empty A
3 1 fill A from B
0 1 empty A
1 0 fill A from B
1 7 fill B
3 5 fill A from B
This leaves 5 gallons in container B after 8 steps.
1 mile = 1760 yards = 1760 x 3 feet = 1760 x 3 x 12 inches = 1760 x 3 x 12 x 25.4 millimetres = 1,609,344mm in 1 mile.
And:
2 kilometres = 2000 metres = 2000 x 100 centimetre = 2000 x 100 x 10 millimetres = 2000 x 100 x 10 ÷ 25.4 inches = 2000 x 100 x 10 ÷ 25.4 x 16 = sixteenths of an inch ≈ 1,259,843 sixteenths of an inch in 2 kilometres.