[Question]
  [
The turtle wants to move along the grid to get to his food. He wants to know how many moves it will take for him to get there.
As well since he is slow he has teleporters set up around his domain that he will utilize if it shortens his path. Or avoid them if it lengthens his path.
## *Meet the turtle*
`üê¢`
The turtle lives on a grid
$$\begin{matrix}
X&X&X&X&X\\
X&X&X&X&X\\
X&X&üê¢&X&X\\
X&X&X&X&X\\
X&X&X&X&X\\
\end{matrix}$$
The turtle can move to any adjacent square...
$$\begin{matrix}
X&X&X&X&X\\
X&\nwarrow&\uparrow&\nearrow&X\\
X&\leftarrow&üê¢&\rightarrow&X\\
X&\swarrow&\downarrow&\searrow&X\\
X&X&X&X&X\\
\end{matrix}$$
However, the turtle cannot move to a square with a mountain
$$\begin{matrix}
X&üåÑ&X&X&X\\
X&\nwarrow&\uparrow&\nearrow&X\\
X&üåÑ&üê¢&\rightarrow&X\\
X&üåÑ&\downarrow&\searrow&X\\
X&üåÑ&X&X&X\\
\end{matrix}$$
The Turtle wants to eat his Strawberry, and would like to know how long it will take to get to his Strawberry
$$\begin{matrix}
X&üåÑ&üçì\\
üê¢&üåÑ&X\\
X&üåÑ&X\\
X&X&X\\
\end{matrix}$$
This example would take the turtle \$5\$ turns
$$\begin{matrix}
X&üåÑ&üçì\\
\downarrow&üåÑ&\uparrow\\
\searrow&üåÑ&\uparrow\\
X&\nearrow&X\\
\end{matrix}$$
Luckily, the Turtle found a teleporter! There are two teleports on the grid that map to each other. Stepping on the teleporter immediately moves the turtle to the corresponding teleporter. Teleporters are very unstable and after using them once, they disapear and are no longer useable.
$$\begin{matrix}
üîµ&üåÑ&üçì\\
üê¢&üåÑ&üî¥\\
X&üåÑ&X\\
X&X&X\\
\end{matrix}$$
It is now faster for the turtle to move up twice. Now the turtles shortest path is \$2\$
$$\begin{matrix}
üîµ&üåÑ&üê¢\\
\uparrow&üåÑ&üî¥\uparrow\\
X&üåÑ&X\\
X&X&X\\
\end{matrix}$$
## The challenge
Given an initial grid configuration output the number of moves it will take the turtle to reach his strawberry.
## Rules
* You may assume that the input grid has a solution
* Each grid will only have one `strawberry` and two `portals` and one `turtle`
* The input grid may be entered in any convenient format
* You should treat `teleporters` are single use items
* The turn that the turtle moves onto a `teleporter` square he is already on the corresponding `teleporter`. He never moves onto a `teleporter` and stays there for a move
* The shortest path does not need to make use of the portal
* The turtle cannot pass into mountain tiles
* You may use any ASCII character or integer to represent `mountains`, `turtle`, `empty grid square`, `strawberry`
* You may use either the same character or two different ASCII characters or integers to represent the `teleporter` pairs
* A grid can have more than one path with the same shortest path length
* This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'")
## Clarifications to Rules
* You should treat `teleporters` are single use items.
**Reasoning**: It was pointed out that the case of:
$$\begin{matrix}
üê¢&X&üîµ&X&üçì\\
üåÑ&üåÑ&üåÑ&üåÑ&üåÑ&\\
üî¥&X&X&X&X
\end{matrix}$$
Could be only solved by entering and exiting the portals twice. At the time of making this clarification both solutions acted by assuming they were either single use, or there was no reason to try previously used squares. To avoid breaking their hard-worked solutions, this seemed the best way account for this set up. Therefore, this would be considered an invalid grid.
## Test Cases formatted as lists
```
[ ['T', 'X', 'X', 'S', 'X'], ['X', 'X', 'X', 'X', 'X'], ['X', 'X', 'X', 'X', 'X'] ] --> 3
[ ['T', 'M', 'X', 'S', 'X'], ['X', 'M', 'X', 'X', 'X'], ['O', 'X', 'X', 'X', 'O'] ] --> 4
[ ['T', 'M', 'X', 'S', 'O'], ['O', 'M', 'X', 'X', 'X'], ['X', 'X', 'X', 'X', 'X'] ] --> 2
[ ['T', 'M', 'X', 'S', 'X'], ['O', 'M', 'X', 'X', 'X'], ['O', 'X', 'X', 'X', 'X'] ] --> 4
[ ['T', 'M', 'S', 'X', 'O'], ['X', 'M', 'M', 'M', 'M'], ['X', 'X', 'X', 'X', 'O'] ] --> 7
[ ['T', 'X', 'X', 'S', 'X'], ['O', 'M', 'M', 'M', 'X'], ['X', 'X', 'O', 'X', 'X'] ] --> 3
```
## Test Cases formatted for humans
```
T X X S X
X X X X X
X X X X X --> 3
T M X S X
X M X X X
O X X X O --> 4
T M X S O
O M X X X
X X X X X --> 2
T M X S X
O M X X X
O X X X X --> 4
T M S X O
X M M M M
X X X X O --> 7
T X X S X
O M M M X
X X O X X --> 3
```
**Credits**
Design and structure via: [*Hungry mouse by Arnauld*](https://codegolf.stackexchange.com/q/176251/79844)
Proposed Challenges Edit Advice: [*Kamil-drakari*](https://codegolf.meta.stackexchange.com/users/71434/kamil-drakari), [*beefster*](https://codegolf.meta.stackexchange.com/users/45613/beefster)
General Edit Advice: [*okx*](https://codegolf.stackexchange.com/users/26600/okx) [*nedla2004*](https://codegolf.stackexchange.com/users/59363/nedla2004) [*mbomb007*](https://codegolf.stackexchange.com/users/34718/mbomb007)
  
[Answer]
# JavaScript (ES7), ~~¬†140 139¬†~~ 138 bytes
Takes input as a matrix of integers with the following mapping:
* \$-1\$ = üîµ (any portal)
* \$0\$ = \$X\$ (empty)
* \$1\$ = üåÑ (mountain)
* \$2\$ = üê¢ (turtle)
* \$3\$ = üçì (strawberry)
```
m=>(R=g=(t,X,Y,i)=>m.map((r,y)=>r.map((v,x)=>r[(u=0,t?v-t:(x-X)**2+(y-Y)**2<3?v-3?~v?v:u--:R=R<i?R:i:1)||g(u,x,y,u-~i,r[x]=1),x]=v)))(2)|R
```
[Try it online!](https://tio.run/##bY5Bj4IwEIXv/oreOtW2rrDJJsTKzZtpAh40yMEomhopBoFAlvWvswVl3Rjn0MybfvPmnbbF9rpL1SVjOtlHzUE0sZiBJ44CMrqia6qImMU83l4AUloZkd5FQctWBJCLD5q5BcscKNmKDIfWCCq2bpupbea2eyvcwskZczzhTZXrOcqZkLo@Qk5LWtGc3RRNgzIUE0LNWxBCwCK118yRQDESM/SNdom@JueIn5MjxPyUKA14ozFBI4RbApvmAPegaTsIOOdp2GndAXyx9DFXeh@V8gDaHOmWO5OfwWAOAcJLzn2OqYHbejYoJGg8RvaDWvTU4h31@aRk@ynfUtaL1x8l33r5XN4vmuq9ZE99vaSXBnoElP/SN78 "JavaScript (Node.js) ‚Äì Try It Online")
### How?
The main recursive search function \$g\$ is able to either look for a specific tile \$t\$ on the board (if it's called with \$t\ne 0\$) or for any tile at \$(x,y)\$ which can be reached from the current position \$(X,Y)\$.
It keeps track of the length of the current path in \$i\$ and updates the best result \$R\$ to \$\min(R,i)\$ whenever the turtle finds the strawberry.
It is first called with \$t=2\$ to find the starting position of the turtle.
It calls itself with \$t=-1\$ if a portal is reached, so that the turtle is teleported to the other portal. We do not increment \$i\$ during such an iteration.
Each visited tile is temporarily set to a mountain to prevent the turtle from moving twice on the same tile in the same path. If we get trapped in a dead-end, the recursion simply stops without updating \$R\$.
### Commented
```
m => (                        // m[] = input matrix
  R =                         // initialize R to a non-numeric value
  g = (t, X, Y, i) =>         // g = recursive search function taking t = expected tile,
                              //     (X, Y) = current coordinates, i = path length
    m.map((r, y) =>           // for each row r[] at position y in m[]:
      r.map((v, x) =>         //   for each tile v at position x in r[]:
        r[                    //     this statement will eventually restore r[x] to v
          ( u = 0,            //     u = next tile to look for, or 0 if none
            t ?               //     if we're looking for a specific tile:
              v - t           //       test whether we've found it
            :                 //     else:
              (x - X) ** 2 +  //       compute the squared Euclidean distance between
              (y - Y) ** 2    //       (x, y) and (X, Y)
              < 3 ?           //       if it's less than 3 (i.e. reachable from (X, Y)):
                v - 3 ?       //         if v is not equal to 3:
                  ~v ?        //           if v is not equal to -1:
                    v         //             test if v = 0
                  :           //           else (v = -1):
                    u--       //             set u = -1 to find the other portal
                :             //         else (v = 3):
                  R = R < i ? //           we've found the strawberry: set R = min(R, i)
                      R : i   //
              :               //       else (this tile can't be reached):
                1             //         yield 1
          ) ||                //     if the above result is falsy:
          g(                  //       do a recursive call:
            u,                //         t = u
            x, y,             //         move to (x, y)
            u - ~i,           //         unless u is set to -1, increment i
            r[x] = 1          //         set this tile to a mountain
          ),                  //       end of recursive call
          x                   //     restore r[x] ...
        ] = v                 //     ... to v
    ))                        // end of both map() loops
)(2) | R                      // initial call to g with t = 2; return R
```
[Answer]
# [Python 2](https://docs.python.org/2/), ~~441~~ ~~431~~ 341 bytes
```
from itertools import*
G=input()
W=len(G[0])
H=len(G)
A=[0]*5
E=enumerate
for y,r in E(G):
 for x,C in E(r):A[C]=[x,y]
for L in count():
 for M in product(*[zip('UDLR'*2,'LRDU    ')]*L):
  x,y=A[0]
  for m in M:
    x+='R'in m;x-='L'in m;y+='D'in m;y-='U'in m
    if(x,y)==A[3]:x,y=A[2]
    if 1-(W>x>-1<y<H)or G[y][x]>3:break
  if[x,y]==A[1]:exit(L)
```
[Try it online!](https://tio.run/##TY8xb8MgEIXn8CvYABdLsdMubogUJVEyOEukqANiSFOsosbGoliC/nn3iKOqLLz7uPfu6KP/tF05jo2zLTZeO2/t7RubtrfOZ2gvTNcPnjL0Jm66o3s5VwwdJs3QWkCdvaCd0N3QanfxGjXW4cgdNh3eQU@FcCKBbybiWLWWGyVk4FHdm@v0cLVDB2Me3ceEemc/hqunmfwxPSXnbX0iWclJfdqeMRzCVFYnB4RHsYZNQCZ3m9zHCs3CkyAnAkX7GnJB6klGoNuHBHq@SzQzDYUcJiBpoaopslSJ4yKnb6uwyotlXB4YTNjLqGRQq0X17vTlC@aa5v6h5C5UpYPxtGbjKOWc42eO84LjIl2KY5mKB8v/4OI/4LhU6hc)
Input as lists, but using numbers instead of characters (thanks to Quintec) and a seperate value for the destination of the teleporter. Those large indentations should be tab characters if Stack Exchange removes them. Any tips or ideas especially welcome, as I feel that this could get ~~**much**~~ somewhat shorter.
The table for characters used in the challenge to the numbers used for my program is below, but you can also use [this program](https://tio.run/##hZBPC4JAEMXv@ykGOujGBmadAm@FeggPCgnLEkFqQq0iRvnpbUdN@wM6h2H3vfk9ZjevyksmzbqegYwekMr8XkKcFbdTSUhSpGewWlGn5OBuA4eBs3NtJ1D6NZI6jnBDUNbfKFFBdktyvlgKmENDYiwcVRoUJ5lEeptDBUG9@tM3BFSh9xy8JqizsNIYmg0qwZ8CLAu0QBtcrG6Z9wQYvRtd/2l/gl6O0rsJ2hylvQl6NUrvJ@g1yYtUlnqn07rmwNVvMURVC7H57UkwZYXfVvhheb8qw7cLEC8).
```
Challenge | My program
T         | 0
S         | 1
E         | 2
O         | 3
M         | 4
X         | -1
```
-10 bytes thanks to Quintec by changing the input from using characters to numbers.
-A lot of bytes thanks to Jonathan Frech, ElPedro, and Jonathan Allan.
[Answer]
# [Python 2](https://docs.python.org/2/), 391 ~~397~~ ~~403~~ ~~422~~ bytes
```
M=input()
from networkx import*
a=b=c=d=0
N,h,w,S=[-1,0,1],len(M),len(M[0]),[]
for i in range(h):
 for j in range(w):
  I,m=(i,j),M[i][j]
  if m>7:c,d=a,b;a,b=I
  if m<0:Z=I
  if m==5:F=I
  S+=[I+I]
S+=[(a,b,c,d),(c,d,a,b)]
print len(shortest_path(from_edgelist([((A+p,B+q),(C,D))for A,B,C,D in S for p,q in[(p,q)for p in N for q in N]if-1<A+p<h and-1<B+q<w and M[C][D]*M[A+p][B+q]]),Z,F))-1
```
[Try it online!](https://tio.run/##RVBLi8IwEL73VwT2ktgptAvi0m0EHwg91Is3Q5BqWxvXprFm6e6v706quJBkvkdmmBnza@tWvw9DxpU235Yyr@rahujS9m339UNUY9rOTrycH/mJFzz0tlBDDzsugghCiCRcS00z9ggilAyE9Kq2I4ooTbpcn0tas9gjTrv8a73TSAoNpwouDDKhpLhI1FRFmvksPkHBczh@4uXpU07CeP8inE/jzch2Phepn0rPAYoJgMkMKL6AjEnPdEpb4nq81zhQebcHk9uaumkPZXEur@puqaB04RtY@jdMXsGaMdf0ApaAxLW@G6cwcEMiKMbxg3HWdrRuI5SqCqIESyU1yXWBGEsmvcMkEysp1nKSCfSlQEPizvawYSyIhkG4vZIQyBQIgg8JRDyE53kKr/Mh5VsQzMnsDw "Python 2 ‚Äì Try It Online")
The problem is translated into a graph and the solution is to find the shortest path form the turtle to the strawberry.
```
Challenge | This code
T         | -1
S         |  5
O         |  8
M         |  0
X         |  1
```
]