I am trying to build a bit more complex train setup... and failing

I have a remote base (R), connected by railway to main base (M). I have to ship material to R, it will produce goods, and ship them back to M.
Because of space constraints, I have to do this by using only two stations: one per base. I also would like to use only one train, to avoid unnecessary congestion on the railway.
The problem is: if I use inserters on a side of the train to load items, and then I put a second row of inserters on the other side to unload them, the inserters will unload items as soon as they get loaded. I cannot use smart inserters to filter what gets unloaded, because I am mixing a wide variety of items into the same wagon.
In short, what I would like to do is to avoid the items that have just been inserted in a wagon to be unloaded until the train stops to next station.
Any ideas on how to handle this?