Food on the Plane
Contest: Canada Cup 2016
Difficulty rating: 1200
Tags: implementation, math

A new airplane SuperPuperJet has an infinite number of rows, numbered with positive integers starting with 1 from cockpit to tail. There are six seats in each row, denoted with letters from 'a' to 'f'. Seats 'a', 'b' and 'c' are located to the left of an aisle (if one looks in the direction of the cockpit), while seats 'd', 'e' and 'f' are located to the right. Seats 'a' and 'f' are located near the windows, while seats 'c' and 'd' are located near the aisle.

It's lunch time and two flight attendants have just started to serve food. They move from the first rows to the tail, always maintaining a distance of two rows from each other because of the food trolley. Thus, at the beginning the first attendant serves row 1 while the second attendant serves row 3. When both rows are done they move one row forward: the first attendant serves row 2 while the second attendant serves row 4. Then they move three rows forward and the first attendant serves row 5 while the second attendant serves row 7. Then they move one row forward again and so on.

Flight attendants work with the same speed: it takes exactly 1 second to serve one passenger and 1 second to move one row forward. Each attendant first serves the passengers on the seats to the right of the aisle and then serves passengers on the seats to the left of the aisle (if one looks in the direction of the cockpit). Moreover, they always serve passengers in order from the window to the aisle. Thus, the first passenger to receive food in each row is located in seat 'f', and the last one — in seat 'c'. Assume that all seats are occupied.

Vasya has seat s in row n and wants to know how many seconds will pass before he gets his lunch.

Input format:
The only line of input contains a description of Vasya's seat in the format ns, where n (1 ≤ n ≤ 1018) is the index of the row and s is the seat in this row, denoted as letter from 'a' to 'f'. The index of the row and the seat are not separated by a space.

Output format:
Print one integer — the number of seconds Vasya has to wait until he gets his lunch.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the first sample, the first flight attendant serves Vasya first, so Vasya gets his lunch after 1 second.

In the second sample, the flight attendants will spend 6 seconds to serve everyone in the rows 1 and 3, then they will move one row forward in 1 second. As they first serve seats located to the right of the aisle in order from window to aisle, Vasya has to wait 3 more seconds. The total is 6 + 1 + 3 = 10.

Example input:
1f

Example output:
1

Example input:
2d

Example output:
10

Editorial:
The only observation one has to make to solve this problem is that flight attendants move 4 rows forward at 16 seconds. Here, 6 seconds are required to serve everyone in one row, then 1 second to move forward, 6 seconds to serve one more row, then 3 more seconds to move. Thus, 6 + 1 + 6 + 3 = 16 in total.
Now, if we are interested in seat c in row x, the answer is equal to the answer for seat c in row xmod4 + 1 plus $$\left\lfloor \frac{x-1}{4} \right\rfloor \cdot 16$$. Note, that we use integer division, so it's not equal to (x - 1)·4.
Solution: compute the serving time for first 4 rows. Find the answer using formula above.