Today s kilometer long auto race takes place in Berland. The track is represented by a straight line as long as s kilometers. There are n cars taking part in the race, all of them start simultaneously at the very beginning of the track. For every car is known its behavior — the system of segments on each of which the speed of the car is constant. The j-th segment of the i-th car is pair (vi, j, ti, j), where vi, j is the car's speed on the whole segment in kilometers per hour and ti, j is for how many hours the car had been driving at that speed. The segments are given in the order in which they are "being driven on" by the cars.

Your task is to find out how many times during the race some car managed to have a lead over another car. A lead is considered a situation when one car appears in front of another car. It is known, that all the leads happen instantly, i. e. there are no such time segment of positive length, during which some two cars drive "together". At one moment of time on one and the same point several leads may appear. In this case all of them should be taken individually. Meetings of cars at the start and finish are not considered to be counted as leads.

Input

The first line contains two integers n and s (2 ≤ n ≤ 100, 1 ≤ s ≤ 106) — the number of cars and the length of the track in kilometers. Then follow n lines — the description of the system of segments for each car. Every description starts with integer k (1 ≤ k ≤ 100) — the number of segments in the system. Then k space-separated pairs of integers are written. Each pair is the speed and time of the segment. These integers are positive and don't exceed 1000. It is guaranteed, that the sum of lengths of all segments (in kilometers) for each car equals to s; and all the leads happen instantly.

Output

Print the single number — the number of times some car managed to take the lead over another car during the race.

Examples

Input

2 33
2 5 1 2 14
1 3 11


Output

1


Input

2 33
2 1 3 10 3
1 11 3


Output

0


Input

5 33
2 1 3 3 10
1 11 3
2 5 3 3 6
2 3 1 10 3
2 6 3 3 5


Output

2
import java.util.*;

public class P043E {
    private static class BIT {
        private final int N;
        private int[] tree;
        
        public BIT(int N) {
            this.N = N;
            tree = new int[N + 1];
        }
        
        private void set(int x, int v) {
            while (x <= N) {
                tree[x] += v;
                x += (x & -x);
            }
        }
        
        private int get(int x) {
            int res = 0;
            while (x != 0) {
                res += tree[x];
                x -= (x & -x);
            }
            return res;
        }
    }
        
    private static class Car {
        private int v;
        private int x;
        private final int id;
        
        public Car(int v, int id) {
            this.v  = v;
            this.x  = 0;
            this.id = id;
        }
    }
    
    private static class Event implements Comparable<Event> {
        private int carId;
        private int v;
        
        public Event(int carId, int v) {
            this.carId = carId;
            this.v     = v;
        }
        
        public int compareTo(Event other) {
            if (carId < other.carId) return -1;
            if (carId > other.carId) return 1;
            if (v < other.v) return -1;
            if (v > other.v) return 1;
            return 0;
        }
    }
    
    private static int intComp(int i, int j) {
        if (i < j) return -1;
        if (i > j) return 1;
        return 0;
    }
    
    private class SortBySpeed implements Comparator<Integer> {             
        public int compare(Integer i, Integer j) {
            int cmp1 = -intComp(cars[i].v, cars[j].v);
            if (cmp1 != 0) return cmp1;
            return intComp(cars[i].id, cars[j].id);
        }
        
    }
    
    private class SortByPosition implements Comparator<Integer> {
        
        public int compare(Integer i, Integer j) {
            int cmp1 = -intComp(cars[i].x, cars[j].x);
            if (cmp1 != 0) return cmp1;
            return intComp(prevPos.get(i), prevPos.get(j));
        }
    }
    
    
    private final int N;
    private int prevT = 0;
    private int currT = 0;
    private TreeMap<Integer, TreeSet<Event>> queue = new TreeMap<Integer, TreeSet<Event>>();
    private HashMap<Integer, Integer> prevPos = new HashMap<Integer, Integer>();
    private Car[] cars;
    
    public P043E() {
        Scanner sc = new Scanner(System.in);
        N = sc.nextInt();
        int s = sc.nextInt();
        
        cars = new Car[N];
        for (int i = 0; i < N; i++) {
            int k = sc.nextInt();
            int v = sc.nextInt();
            int t = sc.nextInt();
            cars[i] = new Car(v, i);
            for (int j = 1; j < k; j++) {
                v = sc.nextInt();
                if (!queue.containsKey(t)) {
                    queue.put(t, new TreeSet<Event>());
                }
                queue.get(t).add(new Event(i, v));
                t += sc.nextInt();
            }
            if (!queue.containsKey(t)) {
                queue.put(t, new TreeSet<Event>());
            }
            queue.get(t).add(new Event(i, 0));
        }
        
        System.out.println(process());
    }
    
    private void updatePosition() {
        for (int i = 0; i < N; i++) {
            cars[i].x += cars[i].v * (currT - prevT);
        }
    }
    
    private void updateSpeed() {
        Map.Entry<Integer, TreeSet<Event>> entry = queue.pollFirstEntry();
        TreeSet<Event> events = entry.getValue();
        for (Event e: events) {
            cars[e.carId].v = e.v;
        }
    }
    
    private int calcNumOfPasses(ArrayList<Integer> prevRank,
                                ArrayList<Integer> currRank) {
        int result = 0;
        BIT bit = new BIT(N);
        for (int i = 0; i < N; i++) {
            int pos = prevPos.get(currRank.get(i));  // previous position in the rank
            if (pos > 0) {
                result += Math.max(0, pos - bit.get(pos));  // how many it passed
            }
            bit.set(pos+1, 1);
        }
        
        return result;
    }
    
    private int process() {
        ArrayList<Integer> prevRank = new ArrayList<Integer>();
        ArrayList<Integer> currRank = new ArrayList<Integer>();
        for (int i = 0; i < N; i++) {
            prevRank.add(i);
            currRank.add(i);
        }
        
        Collections.sort(currRank, this.new SortBySpeed());
        int numOfPasses = 0;
        while (!queue.isEmpty()) {
            prevT = currT;
            currT = queue.firstKey();
            updatePosition();
            updateSpeed();
            
            ArrayList<Integer> tmp = prevRank;
            prevRank = currRank;
            currRank = tmp;
            
            for (int i = 0; i < prevRank.size(); i++) {
                prevPos.put(prevRank.get(i), i);
            }
            Collections.sort(currRank, this.new SortByPosition());
            
            numOfPasses += calcNumOfPasses(prevRank, currRank);
        }
        
        return numOfPasses;
    }
    
    public static void main(String[] args) {
        P043E solution = new P043E();
    }
}
        
        