I need to find the shortest path from top left to bottom right.

The Rules are it has to go from **A** to **B** to **A** to **B** etc.

See picture as example:

[![enter image description here](https://i.stack.imgur.com/DV3YT.png)](https://i.stack.imgur.com/DV3YT.png)

The expected output for the above picture is then **13**.

I'm trying to implement this in java with a dijkstra algorithm for this but then got stuck. Is this the right way to go?