use std::io::{Read, stdin};
use libc::{clock_gettime, timespec, CLOCK_PROCESS_CPUTIME_ID};

fn main() {
    let mut begin = timespec {
        tv_sec: 0,
        tv_nsec: 0,
    };
    unsafe {
        clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &mut begin);
    };

    let mut sum = 0;
    let mut data = String::new();
    stdin().read_to_string(&mut data).unwrap();
    for line in data.lines() {
        let ids = line.split(':').next();
        let after_id = line.split(':').nth(1).unwrap().trim();
        let get_colors = after_id.replace(';', ",");
        let get_colors = get_colors.split(',');
        let mut is_valid = true;
        for get_color in get_colors {
            let color_num: String = get_color
                .chars()
                .filter(char::is_ascii_digit)
                .collect();
            let color_num: i32 = color_num.parse().unwrap();

            if get_color.contains("red") && color_num > 12 {
                is_valid = false;
            }
            if get_color.contains("green") && color_num > 13 {
                is_valid = false;
            }
            if get_color.contains("blue") && color_num > 14 {
                is_valid = false;
            }
        }
        if is_valid {
            for id in ids {
                let id_num: String = id.chars().filter(char::is_ascii_digit).collect();
                let id_num: i32 = id_num.parse().unwrap();
                sum += id_num;
            }
        }
    }
    println!("{sum}");

    let mut end = timespec {
        tv_sec: 0,
        tv_nsec: 0,
    };
    unsafe {
        clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &mut end);
    };
    println!(
        "time: {:.6}ms",
        (end.tv_nsec - begin.tv_nsec) as f32 / 1000.0 / 1000.0
    );
}
