(*
	IV окружной этап Всероссийской олимпиады школьников по информатике
	24 - 28 марта 2006 г.
	г. Петрозаводск

	Задача E. Кенгуренок Лео
	Авторы: А. Николаевский
	Решение: А. Лопатин
*)

{$O-,Q+,R+}
uses SysUtils;
const MaxC=10000;
      MaxH=1000;
      MaxN=1000;
      MaxR=1000;
      MaxM=20000;
      MaxQ=20002;

var q:array [1..MaxQ] of integer;
    l:array [-MaxC..MaxC-1] of integer;
    a, b, qh, qt:integer;

procedure put (x, d:integer);
begin
//  writeln (x, ' ', d);
  if (x<a) or (x>=b) then begin writeln (d); halt end;
  if l[x]>=0 then exit;
  inc (qt);
  q[qt]:=x; l[x]:=d;
end;


function get:integer;
begin
  inc (qh);
  get:=q[qh];
end;


var f:array [-MaxC*2..MaxC*2] of integer;
    g:array [-MaxC*2..MaxC*2] of boolean;
    c:char;
    i, j, h, n, r, ch, hh, m, x, y, z:integer;

begin
  reset (input, 'leo.in'); rewrite (output, 'leo.out');
  assert (not seekeoln); read (a); assert ((a>=-MaxC) and (a<=0));
  assert (not seekeoln); read (b); assert ((b>0) and (b<=MaxC));
  assert (not seekeoln); read (h); assert ((h>=0) and (h<=MaxH));
  assert (not seekeoln); read (n); assert ((n>=2) and (n<=MaxN));
  assert (not seekeoln); read (r); assert ((r>0) and (r<=MaxR));
  assert (not seekeoln); read (m); assert ((m>=0) and (m<=MaxM));
  read (c); assert (c=#13); read (c); assert (c=#10);
  for i:=a-MaxC to a-1 do begin f[i]:=h; g[i]:=true end;
  for i:=b to b+MaxC do begin f[i]:=h; g[i]:=true end;
  for i:=1 to m do begin
    assert (not seekeoln); read (x); assert ((x>=-MaxC) and (x<=MaxC));
    assert (not seekeoln); read (y); assert ((y>=-MaxC) and (y<=MaxC));
    assert (x<y);
    assert (not seekeoln); read (z); assert ((z>=0) and (z<=MaxH));
    for j:=x to y-1 do begin
      assert ((f[j]=0) and (not g[j]));
      f[j]:=z; g[j]:=true;
    end;
    read (c); assert (c=#13); read (c); assert (c=#10);
  end;
  assert (eof);
  fillchar (l, sizeof (l), -1);
  put (0, 0);
  while qh<qt do begin
    x:=get;
    ch:=0;
    for i:=1 to n do begin
      if f[x-i]>ch then ch:=f[x-i];
      if ch<=f[x]+1 then hh:=f[x]+1 else hh:=ch+1;
      if hh-f[x]+i>n then break;
      if f[x]+(n-i)-f[x-i]<=r then put (x-i, l[x]+1);
    end;
    ch:=0;
    for i:=1 to n do begin
      if f[x+i]>ch then ch:=f[x+i];
      if ch+1<=f[x]+1 then hh:=f[x]+1 else hh:=ch+1;
      if hh-f[x]+i>n then break;
      if f[x]+(n-i)-f[x+i]<=r then put (x+i, l[x]+1);
    end;
  end;
  writeln (0);
end.