Distance=speed times time so time=distance divided by speed.
Let f=speed of freight train, then the speed of the passenger train is f+12.
310/(f+12)=250/f
Cross-multiply:
310f=250(f+12)=250f+3000, 310f-250f=3000, 60f=3000, f=3000/60=50mph.
So, since the speed of the freight train is 50mph, the speed of the passenger train is 50+12=62mph.
It takes 250/50=5 hours for the trains to complete their journeys.