import Control.Monad
import Data.List
import Data.List.Split

leaps y = y `div` 4 - y `div` 100 + y `div` 400
isLeap 0 = False
isLeap y = y `mod` 4 == 0 && (y `mod` 100 /= 0 || y `mod` 400 == 0)
y2d y = y * 365 + leaps y

calendar = [31,28,31,30,31,30,31,31,30,31,30,31]
mon2d m = sum $ genericTake (m - 1) calendar

g2d y m d
 | isLeap y && m <= 2 = x - 1
 | otherwise = x
 where x = y2d y + mon2d m + d

divModIf x (y,z)
 | p == z = (p - 1, q + y)
 | otherwise = t
 where t@(p,q) = divMod x y

d2y x = (a * 400 + c * 100 + e * 4 + g, h)
 where 
  (a,b) = x `divMod` (365 * 400 + 100 - 4 + 1)
  (c,d) = b `divModIf` (365 * 100 + 25 - 1, 4)
  (e,f) = d `divMod` (365 * 4 + 1)
  (g,h) = f `divModIf` (365, 4)

d2mon x = f x 1 calendar
 where
  f x y (z:zs)
   | x <= z = (y, x)
   | otherwise = f (x - z) (y + 1) zs

leapDay = mon2d 3

d2g x
 | a == 0 = [y - 1, 12, 31]
 | not (isLeap y) = [y, m, d]
 | a == leapDay = [y, 2, 29]
 | a > leapDay = [y, m, d]
 | otherwise = [y, m', d']
 where 
  (y,a) = d2y x
  (m,d) = d2mon a
  (m',d') = d2mon (a + 1)

n2xn _ ys [] = ys
n2xn x ys (z:zs) = n2xn p (q : ys) zs
 where (p,q) = divMod x z
xn2n ys zs = f 1 ys zs
 where
  f _ [] _ = 0
  f x (y:ys) (z:zs) = x * y + f (x * z) ys zs

mds = [20,18,20,20,13]
mbase = g2d 2012 12 21
m2d b ka t w ki = mbase + xn2n [ki,w,t,ka,b] mds
mmax = m2d 12 19 19 17 19 - mbase
d2m x = n2xn ((x - mbase) `mod` (mmax + 1)) [] mds

main = do
 l <- getLine
 when (l /= "#") $ do
  putStrLn $ intercalate "." $ map show $ 
   case map read $ splitOn "." $ l of
    [y,m,d] -> d2m $ g2d y m d 
    [b,ka,t,w,ki] -> d2g $ m2d b ka t w ki
  main