Problem: Polycarpus has a hobby — he develops an unusual social network. His work is almost completed, and there is only one more module to implement — the module which determines friends. Oh yes, in this social network one won't have to add friends manually! Pairs of friends are deduced in the following way. Let's assume that user A sent user B a message at time t1, and user B sent user A a message at time t2. If 0 < t2 - t1 ≤ d, then user B's message was an answer to user A's one. Users A and B are considered to be friends if A answered at least one B's message or B answered at least one A's message.

You are given the log of messages in chronological order and a number d. Find all pairs of users who will be considered to be friends.

Input

The first line of the input contains two integers n and d (1 ≤ n, d ≤ 1000). The next n lines contain the messages log. The i-th line contains one line of the log formatted as "Ai Bi ti" (without the quotes), which means that user Ai sent a message to user Bi at time ti (1 ≤ i ≤ n). Ai and Bi are non-empty strings at most 20 characters long, consisting of lowercase letters ('a' ... 'z'), and ti is an integer (0 ≤ ti ≤ 10000). It is guaranteed that the lines are given in non-decreasing order of ti's and that no user sent a message to himself. The elements in the lines are separated by single spaces.

Output

In the first line print integer k — the number of pairs of friends. In the next k lines print pairs of friends as "Ai Bi" (without the quotes). You can print users in pairs and the pairs themselves in any order. Each pair must be printed exactly once.

Examples

Input

4 1
vasya petya 1
petya vasya 2
anya ivan 2
ivan anya 4


Output

1
petya vasya


Input

1 1000
a b 0


Output

0

Note

In the first sample test case Vasya and Petya are friends because their messages' sending times are one second apart. Anya and Ivan are not, because their messages' sending times differ by more than one second.

Solution (in JAVA):

Solution:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.StringTokenizer;

/**
 *
 * @author HronoS
 */
public class vk1 implements Runnable {
  
  StringTokenizer st;
  BufferedReader in;
  PrintWriter out;
  
  public static void main(String[] args) {
    new Thread(new vk1()).start();
  }
  
  public void run() {
    try {
      boolean oj = System.getProperty("ONLINE_JUDGE") != null;
      Reader reader = oj ? new InputStreamReader(System.in) : new FileReader("vk1.in");
      Writer writer = oj ? new OutputStreamWriter(System.out) : new FileWriter("vk1.out");
      
      in = new BufferedReader(reader);
      out = new PrintWriter(writer);
      
      solve();
    } catch (Exception e) {
      System.exit(9000);
    } finally {
      out.flush();
      out.close();
    }
  }
  
  String nextToken() throws IOException {
    while (st == null || !st.hasMoreTokens()) {
      st = new StringTokenizer(in.readLine());
    }
    return st.nextToken();
  }
  
  int nextInt() throws NumberFormatException, IOException {
    return Integer.parseInt(nextToken());
  }
  
  long nextLong() throws NumberFormatException, IOException {
    return Long.parseLong(nextToken());
  }
  
  double nextDouble() throws NumberFormatException, IOException {
    return Double.parseDouble(nextToken());
  }
  
  class Pair{
    String name1;
    String name2;
    Pair(String name1, String name2){
      if(name1.compareTo(name2) < 0) {
        this.name1 = name1;
        this.name2 = name2;
      } else {
        this.name1 = name2;
        this.name2 = name1;
      }
    }

    @Override
    public boolean equals(Object obj) {
      if (obj == null) {
        return false;
      }
      if (getClass() != obj.getClass()) {
        return false;
      }
      final Pair other = (Pair) obj;
      if ((this.name1 == null) ? (other.name1 != null) : !this.name1.equals(other.name1)) {
        return false;
      }
      if ((this.name2 == null) ? (other.name2 != null) : !this.name2.equals(other.name2)) {
        return false;
      }
      return true;
    }
    
    

    @Override
    public int hashCode() {
      int hash = 7;
      hash = 97 * hash + (this.name1 != null ? this.name1.hashCode() : 0);
      hash = 97 * hash + (this.name2 != null ? this.name2.hashCode() : 0);
      return hash;
    }
  }
  
  void solve() throws NumberFormatException, IOException {
    int n = nextInt();
    int d = nextInt();
    HashMap<String, HashMap<String, ArrayList<Integer>>> data = new HashMap<String, HashMap<String, ArrayList<Integer>>>();
    HashSet<Pair> answer = new HashSet<Pair>();
    String name1 = null;
    String name2 = null;
    int time = -1;
    int savedTime = -1;
    for (int i = 0; i < n; i++) {
      name1 = nextToken();
      name2 = nextToken();
      time = nextInt();
      
      if(data.get(name2) != null) {
        ArrayList<Integer> times = data.get(name2).get(name1);
        if(times != null){
          for(int j = times.size() - 1 ; j>=0 ; j--){
           savedTime = times.get(j);
           if((time - savedTime > 0) && (time - savedTime <= d )){
             answer.add(new Pair(name1, name2));
             break;
           } 
          }
        }
      }
      
      HashMap<String, ArrayList<Integer>> innerData = data.get(name1);
      if(innerData == null){
        innerData = new HashMap<String, ArrayList<Integer>>();
        data.put(name1, innerData);
      }
      ArrayList<Integer> times = innerData.get(name2);
      if(times == null){
        times = new ArrayList<Integer>();
        innerData.put(name2, times);
      }
      times.add(time);
    }
    out.println(answer.size());
    for(Pair pair:answer){
      out.printf("%s %s\n", pair.name1, pair.name2);
    }
  }
}