Under construction

Still being built. Things may change

Column Addition

Time limit2sMemory limit512 MB

Summary
Given three n-digit strings, erase the fewest digit columns so the first number plus the second equals the third.
Level

Medium7 of 10

Topics
Dynamic programming, String, Math, Implementation
Solved
No attempts yet

Problem

A column addition adds two integers written one above the other with their digits aligned.

   123
 + 456
-------
   579

A column addition is written on the blackboard, but the sum it shows is not necessarily correct. You may erase any number of columns so that the addition becomes correct. In the addition below, erasing the second column and the fourth column leaves a correct addition.

   12127           117
 + 45618    ⇒    + 468
---------       -------
   51825           585

Read the kept columns from left to right to form three numbers. The addition is correct when the first number plus the second number equals the third number. Erasing every column leaves three empty numbers, and that counts as correct.

Find the minimum number of columns you have to erase so that the remaining formula is a correct addition.

Input

The input holds several test cases. Each test case starts with a line holding a single integer n, the number of digit columns (1≤n≤10001 \le n \le 1000). Each of the next three lines holds a string of n digits. The number on the third line is written as the sum of the numbers on the first two lines, but it is not necessarily correct. The input ends with a line holding a single 0, which you do not process.

Output

For each test case, print the minimum number of columns that have to be erased on its own line.

Examples2

  1. Example 1

    Input
    3
    123
    456
    579
    5
    12127
    45618
    51825
    2
    24
    32
    32
    5
    12299
    12299
    25598
    0
    
    Expected output
    0
    2
    2
    1
    
  2. Example 2

    Input
    1
    0
    0
    0
    1
    5
    5
    0
    1
    9
    9
    8
    1
    1
    2
    3
    0
    
    Expected output
    0
    1
    1
    0