What happened? Where is it? You wonder, waking up only to find yourself in a strange but familiar place. You look around, and the sign on the wall immediately reminds you where you are. The sign says “Aperture Science Enrichment Center”.
Hi Chell, glad to see you again. It is GLaDOS with her electronic voice. She seems excited to see you,
You may wonder why you are here again. The truth is, after some failed trials, I finally realized that you are the one and only great test subject! Aperture Science needs you! You look frightened by her outburst. But she then goes back to a gentle, soft voice,
Don't be afraid, all you need to do is complete some tests in some rooms, but this time you should make it as fast as possible. When all tests are done, you can have a cake as an award.

GLaDOS at the Aperture Science Enrichment Center
You don't know whether you can trust her, but you simply have no choice but to complete all tests as fast as possible. The Aperture Science Enrichment Center has

distinct rooms, labeled

through

. Thanks to recent developments in the years when you were absent, now you can directly go from any room

to room
)
within time

. You may assume that

for all

and

for all

satisfying

. Also, as there exist one-way elevators, gels, companion cubes, and bots, it could happen that

for

. GLaDOS has handed you a list of rooms
)
so that you need to sequentially visit the rooms in the list
in order, to complete all tests as fast as possible. You are now in room

and are ready to take the first test at any time. As you are so familiar with all the tests, the time needed for you to complete each test can be ignored, and you only need to minimize the total time cost on the ways of visiting the rooms, which sounds easy enough.
Oh. I forgot to remind you, to make it easier for you but not too easy, you may look behind you. You turned around, and there is a portal gun in the box, but on the box, it says “Limited use: only use once before starting the tests”. With closer inspection, this portal gun creates portals not by shooting but by entering room numbers. The guideline lying next to it says that before trying to complete all tests, you are allowed to enter two numbers

such that

, then a two-way portal will be created between rooms

and

, so that you can directly move from either one to the other within no time, meaning setting

. This choice of

and

needs to be made before trying to complete all the tests and cannot be changed.
So you need to carefully choose the two rooms to open up a portal, then complete all tests as quickly as possible. What is the minimum time needed? Surely you can find out.