my $input = <>;
chomp ($input);

my @input = split (/ /, $input);

my $sleeps = $input[0];
my $hour = $input[1];
my $minutes = $input[2];
my $interval = $input[3];


#何度寝と間隔から時間を求める
if ($sleeps > 0){
    $sleeps -= 1;
    my $get_up_time = $interval * $sleeps;
    $minutes += $get_up_time;
    if ($minutes >= 60){
        my $count_hour;
        for ($count_hour = 0; $minutes >= 60; $count_hour++){
            $minutes -= 60;
        }

        $hour += $count_hour;

        if ($hour >= 120){
            for (my $count_day5 = 0; $hour >= 120; $count_day5++){
                $hour -= 120;
            }
        }
        if ($hour >= 24) {
            for (my $count_day = 0; $hour >= 24; $count_day++){
                $hour -= 24;
            }
        }
    }
}

print "$hour\n";
print "$minutes\n";