program zC; {$APPTYPE CONSOLE} uses SysUtils; type time = record h, s, m: string; end; var f, x, y, z, a, b, h1, m1, s1, h2, m2, s2, lh, lm, ls, tt: integer; c: char; n, k: time; function obr(s: string; len: integer): string; begin while (length(s) < len) do s := '0' + s; obr := s; end; function timez(h, m, s: integer): time; begin timez.h := obr(inttostr(h), lh); timez.m := obr(inttostr(m), lm); timez.s := obr(inttostr(s), ls); end; function check(a: time): boolean; begin check := ((pos(c, a.h) <> 0) or (pos(c, a.m) <> 0) or (pos(c, a.s) <> 0)); end; function incs(f: time): time; var h, m, s, t: integer; begin h := strtoint(f.h); m := strtoint(f.m); s := strtoint(f.s); t := h * a * b + m * b + s; inc(t); if (t = tt) then incs := timez(0, 0, 0) else begin h := t div (a * b); m := (t - h * a * b)div b; s := (t - h * a * b - m * b); incs := timez(h, m, s); end; end; function ll(k: integer): integer; begin if (k div 10 = 0) then ll := 1 else ll := 2; end; begin readln(a, b); readln(x, y, z); lh := ll(x); lm := ll(y); ls := ll(z); tt := x * a * b + y * b + z; readln(h1, m1, s1); readln(h2, m2, s2); readln(c); n := timez(h1, m1, s1); k := timez(h2, m2, s2); f := 0; while (true) do begin if (check(n)) then inc(f); if ((n.h = k.h) and (n.m = k.m) and (n.s = k.s)) then break; n := incs(n); end; writeln(f); end.