Let the length of the train be L and its speed be S. Using the relative speed formula for the same direction, we get L equals (S minus 2) multiplied by (5/18) multiplied by 9, and L equals (S minus 4) multiplied by (5/18) multiplied by 10. Equating the two expressions gives 5 multiplied by (S minus 2) equals (50/9) multiplied by (S minus 4), which simplifies to 45S minus 90 equals 50S minus 200, resulting in a train speed of 22 km/h. Substituting this speed back gives the train's length as (22 minus 2) multiplied by (5/18) multiplied by 9, which equals 50 meters.