#include<bits/stdc++.h> using namespace std; int main(){ int h,m,s; char t; cin>>h>>m>>s>>t; if(t=='A'){ cout<<h*60*60+m*60+s; } else{ cout<<12*60*60+h*60+m*60+s; } return 0; }