function [value,isterminal,direction] = memoscEvents(t,x,p)
  
  if p.Region==1
    value = [x(4)-0;x(4)-1;x(5)-0;x(5)-1];
    isterminal = [1; 1; 1; 1]; % all events are terminal
    direction = [-1;+1;-1;+1]; % -1/+1 - the value is decreasing/increasing
  end
  if p.Region==2
    value = [x(1);x(5)-1];
    isterminal = [1; 1]; % all events are terminal
    direction = [+1;+1]; % -1/+1 - the value is decreasing/increasing
  end
  if p.Region==3
    value = [x(1);x(5)-0];
    isterminal = [1; 1]; % all events are terminal
    direction = [-1;-1]; % -1/+1 - the value is decreasing/increasing
  end
  if p.Region==4
    value = [x(1);x(4)-1];
    isterminal = [1;1]; % all events are terminal
    direction = [-1;+1]; % -1/+1 - the value is decreasing/increasing
  end
  if p.Region==5
    value = [x(1);x(4)-0];
    isterminal = [1;1]; % all events are terminal
    direction = [+1;-1]; % -1/+1 - the value is decreasing/increasing
  end
  if p.Region==6
    value = [x(1)];
    isterminal = [1]; % all events are terminal
    direction = [+1]; % -1/+1 - the value is decreasing/increasing
  end
  if p.Region==7
    value = [x(1)];
    isterminal = [1]; % all events are terminal
    direction = [-1]; % -1/+1 - the value is decreasing/increasing
  end

