a=int(input()) b=int(input()) c=int(input()) d=input() if d=="a": print(a*60*60+b*60+c) elif d=="p": print(12*60*60+a * 60 * 60 + b * 60 + c)