Under construction

Still being built. Things may change

Longest Palindrome

Interview

Time limit1sMemory limit256 MB

Summary
Pick some of the distinct equal-length strings and order them so the concatenation is the longest possible palindrome, then print one such palindrome.
Level

Medium4 of 10

Topics
Hash map, Greedy, String
Solved
No attempts yet

Problem

Gildong is back to problem solving, and now he is studying palindromes. A palindrome is a string that is the same as its reverse. For example, "pop", "noon", "x", and "kkkkkk" are palindromes, while "moon", "tv", and "abab" are not. An empty string is also a palindrome.

Gildong has nn distinct strings, each of length mm. He wants to discard some of the strings (possibly none or all) and reorder the remaining strings so that their concatenation is a palindrome. He wants the palindrome to be as long as possible. Find one such palindrome.

Input

The first line contains two integers nn and mm (1≤n≤1001 \le n \le 100, 1≤m≤501 \le m \le 50), the number of strings and the length of each string.

The next nn lines each contain one string of length mm, consisting of lowercase Latin letters only. All strings are distinct.

Output

On the first line, print the length of the longest palindrome you made.

On the second line, print that palindrome. If there are multiple answers, print any one of them. If the palindrome is empty, print an empty line or omit this line.

Hint

In the first example, "battab" is also a valid answer.

In the second example, there are 4 different valid answers, including the sample output. The other answers are not listed here.

In the third example, the empty string is the only valid palindrome.

Examples4

  1. Example 1

    Input
    3 3
    tab
    one
    bat
    
    Expected output
    6
    tabbat
    
  2. Example 2

    Input
    4 2
    oo
    ox
    xo
    xx
    
    Expected output
    6
    oxxxxo
    
  3. Example 3

    Input
    3 5
    hello
    codef
    orces
    
    Expected output
    0
    
  4. Example 4

    Input
    9 4
    abab
    baba
    abcd
    bcde
    cdef
    defg
    wxyz
    zyxw
    ijji
    
    Expected output
    20
    ababwxyzijjizyxwbaba