Align the text to a block Task

Your task is to write whole program, which will align given input to a block of given size.

Input:

40
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.


Output:

Lorem  ipsum dolor sit amet, consectetur
adipiscing  elit,  sed do eiusmod tempor
incididunt  ut  labore  et  dolore magna
aliqua.  Ut  enim  ad minim veniam, quis
nostrud   exercitation  ullamco  laboris
nisi ut aliquip ex ea commodo consequat.
Duis  aute  irure dolor in reprehenderit
in voluptate velit esse cillum dolore eu
fugiat  nulla  pariatur.  Excepteur sint
occaecat cupidatat non proident, sunt in
culpa  qui  officia deserunt mollit anim
id est laborum.
                &lt;-- note the linebreak


Input Details


You have to write whole executable/interpretable program.
You may suppose, that the input contains only printable ASCII characters and does not contain tab \t.
The input can contain line breaks. If it does so, they are given as \n, \r, or \r\n depending on what you expect. They are however united throughout whole input.
The input can be given in STDIN, or as command line arguments. You can use both of them if it suits your needs (for example, read block size from command args and input as stdin). You may not, however, hardcode any part of the input to your program.
You may suppose that the block size is given as valid positive (&gt; 0) number.
Line break in the input is treated the same way as a space (thus being word separator)
If the input contains multiple concurrent spaces they are treated as one.


Output details


The output has to be formatted into a block of given size. The formatting is done by adding spaces between words.
The number of spaces between words on a line has to be equal for that line. If it is not possible, additional spaces must be added one by one after words starting from the left.
If the word is longer that given block size, it will stand alone on a single line (and will exceed the block size).
If the output should contain only one word, it will be aligned to the left.
The last line of the output has to be aligned to the left, with only one space between words. The last line of the output has to end with trailing line break.


The answer, with shortest number of bytes after some time, wins.
If you need additional information, leave a comment.

Test cases

Input:
10
Lorem ipsum dolor sit amet,
consectetur adipiscing elit, sed do eiusmod tempor

Output:
Lorem
ipsum
dolor  sit
amet,
consectetur
adipiscing
elit,  sed
do eiusmod
tempor




Input:
20
Lorem     ipsum

dolor  sit amet,

Output:
Lorem   ipsum  dolor
sit amet,




Input:
1
Lorem ipsum dolor sit amet

Output:
Lorem
ipsum
dolor
sit
amet
Pyth, 60 57 54 51 50 52 bytes
2015-10-22: fixing a bug took 2 bytes.
=HfTcjd.zdWHAcH]f|qTlH&gt;jd&lt;HhTQ1s.iGcJ|tlG1*d?H-QlsGJ

Try it online.