Network Analysis, Python, Qgis.
B (blue) I’m going to use the Shortest Path (point to point) tool (found under ‘Network Analysis’ on the Toolbox icon) to do this. This is going to calculate, for every possible route combination, the distance (in km) from A to B, and return the shortest. For the start and end points, I’ve taken