def count_words(sequences, num):
    """
    To count the most common words of sequences.
    sequences: sequences need to be counted
    num: return top num words.
    """

    counts = {}

    # To count words
    for x in sequences:
        if x in counts:
            counts[x] += 1
        else:
            counts[x] = 1

    items = counts.items()    # To get all items with tuple
    sortedItems = tuple(sorted(items, key = lambda element: (element[1],element[0]),reverse = True))    # To sort tuple by values
    # or using this:
    # sortedItems = [(words, count) for words, count in counts.items()].sort(key = lambda x: x[1])

    return sortedItems[:num]