#include <iostream>

using namespace std;

int main(){
    int K, N, igra, T, M, S, LS;
    char Z;
    cin>> M>>S;
    cin >> K;
    cin >> N;
    LS= M*60+S;
    igra = 0;
    while(igra < LS)
    {
        cin >> T >> Z;
        igra += T;
        if ((Z == 'T') && (igra < LS))
            K = (K % 8) + 1;
    }
    cout << K<<endl;

return 0;
}
