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.*;
import java.util.*;

public class Main {
    Map< String, Integer > map = new HashMap< String, Integer >( );
    Map< Integer, String > names = new HashMap< Integer, String >( );
    int cidx = 0;
    final int INF = 1 << 29;

    public void solve( ) throws Throwable {
        boolean ok[ ][ ] = new boolean[ 2001 ][ 2001 ];
        int n = in.nextInt( ), d = in.nextInt( );
        int sent1[ ] = new int[ n ], sent2[ ] = new int[ n ], time[ ] = new int[ n ];

        for ( int i = 0; i < n; ++i ) {
            int j = get( in.next( ) ), k = get( in.next( ) ), t = in.nextInt( );
            sent1[ i ] = j;
            sent2[ i ] = k;
            time[ i ] = t;
        }
outer:
        for ( int i = 0; i < n; ++i ) {
            int min = Math.max( time[ i ] - d, 0 ) ;
            for ( int j = i - 1; j >= 0 && time[ j ] >= min; --j ) {
                if ( time[ j ] != time[ i ] && sent1[ j ] == sent2[ i ] && sent2[ j ] == sent1[ i ] ) {
                    ok[ sent1[ i ] ][ sent2[ i ] ] = ok[ sent2[ i ] ][ sent1[ i ] ] = true;
                    continue outer;
                }
            }

        }
        List< String > f1 = new ArrayList< String >( );
        List< String > f2 = new ArrayList< String >( );

        for ( int i = 0; i < ok.length; ++i ) {
            for ( int j = i + 1; j < ok[ i ].length; ++j ) {
                if ( ok[ i ][ j ] ) {
                    f1.add( names.get( i ) );
                    f2.add( names.get( j ) );
                }
            }
        }
        out.println( f1.size( ) );
        for ( int i = 0; i < f1.size( ); ++i ) {
            out.println( f1.get( i ) + " " + f2.get( i ) );
        }
    }

    int get( String s ) {
        Integer t = map.get( s );
        if ( t == null ) {
            t = cidx;
            map.put( s, t );
            names.put( t, s );
            ++cidx;
        }
        return t;
    }

    public void run( ) {
        in = new FastScanner( System.in );
        out = new PrintWriter( new PrintStream( System.out ), true );

        try {
            solve( );
            out.close( );
            System.exit( 0 );
        } catch( Throwable e ) {
            e.printStackTrace( );
            System.exit( -1 ); 
        }
    }


    public void debug( Object...os ) {
        System.err.println( Arrays.deepToString( os ) );
    }

    public static void main( String[ ] args ) {
        ( new Main( ) ).run( );
    }



    private FastScanner in;
    private PrintWriter out;

    private static class FastScanner {
        private int charsRead;
        private int currentRead;
        private byte buffer[ ] = new byte[ 0x1000 ];
        private InputStream reader;

        public FastScanner( InputStream in ) {
            reader = in;
        }

        public int read( ) {
            if ( charsRead == -1 ) {
                throw  new InputMismatchException( );
            }

            if ( currentRead >= charsRead ) {
                currentRead = 0;
                try {
                    charsRead = reader.read( buffer );

                } catch( IOException e ) {
                    throw new InputMismatchException(  );
                }

                if ( charsRead <= 0 ) {
                    return -1;
                }
            }

            return buffer[ currentRead++ ];
        }

        public int nextInt( ) {
            int c = read( );

            while ( isWhitespace( c ) ) {
                c = read( );
            }

            if ( c == -1 ) {
                throw new NullPointerException( );
            } 

            if ( c != '-' && !( c >= '0' && c <= '9' ) ) {
                throw new InputMismatchException( );
            }

            int sign = c == '-' ? -1 : 1;
            if ( sign == -1 ) {
                c = read( );
            }

            if ( c == -1 || !( c >= '0' && c <= '9' ) ) {
                throw new InputMismatchException( );
            }

            int ans = 0;
            while ( !isWhitespace( c )  && c != -1 ) {
                if ( !( c >= '0' && c <= '9' ) ) {
                    throw  new InputMismatchException( );
                }
                int num = c - '0';
                ans = ans * 10 + num;
                c = read( );
            }

            return ans * sign;
        }

        public long nextLong( ) {
            int c = read( );

            while ( isWhitespace( c ) ) {
                c = read( );
            }

            if ( c == -1 ) {
                throw new NullPointerException( );
            }

            if ( c != '-' && !( c >= '0' && c <= '9' ) ) {
                throw new InputMismatchException( );
            }

            int sign = c == '-' ? -1 : 1;
            if ( sign == -1 ) {
                c = read( );
            }

            if ( c == -1 || !( c >= '0' && c <= '9' ) ) {
                throw new InputMismatchException( );
            }

            long ans = 0;
            while ( !isWhitespace( c )  && c != -1 ) {
                if ( !( c >= '0' && c <= '9' ) ) {
                    throw  new InputMismatchException( );
                }
                int num = c - '0';
                ans = ans * 10 + num;
                c = read( );
            }

            return ans * sign;
        }



        public boolean isWhitespace( int c ) {
            return  c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == -1;
        }

        public String next( ) {
            int c = read( );
            StringBuffer ans = new StringBuffer( );

            while ( isWhitespace( c ) && c != -1 ) {
                c  = read( );
            }

            if ( c == -1 ) {
                return null;
            }

            while ( !isWhitespace( c ) && c != -1 ) {
                ans.appendCodePoint( c );
                c = read( );
            }

            return ans.toString( );

        }

               
        public String nextLine( ) {
            String ans = nextLine0( );

            while ( ans.trim( ).length( ) == 0 ) {
                ans = nextLine0( );
            }

            return ans;
        }
        
        private String nextLine0( ) {
            int c = read( );
            if ( c == -1 ) {
                return null;
            }
            StringBuffer ans = new StringBuffer( );
            while ( c != '\n' && c != '\r' && c != -1 ) {
                ans.appendCodePoint( c );
                c = read( );
            }

            return ans.toString( );

        }


       
        public double nextDouble( ) {
            int c = read( );
            while ( isWhitespace( c ) ) {
                c = read( );
            }

            if ( c == -1 ) {
                throw new NullPointerException( ); 
            }

            if ( c != '.' && c != '-' && !( c >= '0' && c <= '9' ) ) {
                throw new InputMismatchException( );
            }

            int sign = c == '-' ? -1 : 1;
            if ( c == '-' ) {
                c = read( );
            }
            double ans = 0;
            while ( c != -1 && c != '.' && !isWhitespace( c ) ) {
                if ( !( c >= '0' && c <= '9' ) ) {
                    throw new InputMismatchException( );
                }

                int num = c - '0'; 
                ans = ans * 10.0 + num;
                c = read( );
            }

            if ( !isWhitespace( c ) && c != -1 && c != '.' ) {
                throw new InputMismatchException( );
            }

            double pow10 = 1.0;

            if ( c == '.' ) {
                c = read( );
            }

            while ( !isWhitespace( c ) && c != -1 ) {
                pow10 *= 10.0;

                if ( !( c >= '0' && c <= '9' ) ) {
                    throw new InputMismatchException( );
                }
                int num = c - '0';
                ans = ans * 10.0 + num; 
                c = read( );
            }

            return ans * sign / pow10;
        }
    }

}