Problem: You are given a straight half-line divided into segments of unit length, which we will call positions. The positions are numbered by positive integers that start with 1 from the end of half-line, i. e. 1, 2, 3 and so on. The distance between the positions is the absolute difference between the respective numbers. 

Laharl, Etna and Flonne occupy some positions on the half-line and they want to get to the position with the largest possible number. They are originally placed in different positions. 

Each of the characters can perform each of the following actions no more than once: 

  * Move a certain distance. 
  * Grab another character and lift him above the head. 
  * Throw the lifted character a certain distance. 



Each character has a movement range parameter. They can only move to free positions, assuming that distance between those positions doesn't exceed the movement range. 

One character can lift another character if the distance between the two characters equals 1, and no one already holds that another character. We can assume that the lifted character moves to the same position as the person who has lifted him, and the position in which he stood before becomes free. A lifted character cannot perform any actions and the character that holds him cannot walk. 

Also, each character has a throwing range parameter. It is the distance at which this character can throw the one lifted above his head. He can only throw a character to a free position, and only when there is a lifted character. 

We accept the situation when one person grabs another one who in his turn has the third character in his hands. This forms a "column" of three characters. For example, Laharl can hold Etna while Etna holds Flonne. In this case, Etna and the Flonne cannot perform any actions, and Laharl can only throw Etna (together with Flonne) at some distance. 

Laharl, Etna and Flonne perform actions in any order. They perform actions in turns, that is no two of them can do actions at the same time.

Determine the maximum number of position at least one of the characters can reach. That is, such maximal number x so that one of the characters can reach position x.

Input

The first line contains three integers: Laharl's position, his movement range and throwing range. The second and the third lines describe Etna's and Flonne's parameters correspondingly in the similar form. It is guaranteed that the three characters occupy distinct positions. All numbers in the input are between 1 and 10, inclusive.

Output

Print a single number — the maximum ordinal number of position which either Laharl, Etna or Flonne can reach.

Examples

Input

9 3 3
4 3 1
2 3 3


Output

15

Note

Let us explain how to reach position 15 in the sample.

Initially Laharl occupies position 9, Etna — position 4 and Flonne — position 2.

First Laharl moves to position 6.

Then Flonne moves to position 5 and grabs Etna.

Laharl grabs Flonne and throws to position 9.

Flonne throws Etna to position 12.

Etna moves to position 15.

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
struct P {
  int x[3], h[3], mask;
  P() {
    memset(h, -1, sizeof h);
    mask = 0;
  }
  bool operator<(const P &a) const {
    for (int i = 0; i < 3; ++i)
      if (x[i] != a.x[i]) return x[i] < a.x[i];
    for (int i = 0; i < 3; ++i)
      if (h[i] != a.h[i]) return h[i] < a.h[i];
    return mask < a.mask;
  }
  bool operator==(const P &a) const {
    for (int i = 0; i < 3; ++i)
      if (x[i] != a.x[i]) return false;
    for (int i = 0; i < 3; ++i)
      if (h[i] != a.h[i]) return false;
    return mask == a.mask;
  }
};
queue<P> Q;
set<P> store;
int Throw[3], Move[3], Catch[3];
int throwRange[3], moveRange[3], result;
void add(const P &a) {
  if (store.count(a)) return;
  Q.push(a);
  store.insert(a);
  for (int i = 0; i < 3; ++i) result = max(result, a.x[i]);
}
int main() {
  P initState;
  int cnt = 0;
  result = 0;
  for (int i = 0; i < 3; ++i) {
    int initPos;
    scanf("%d%d%d", &initPos, &moveRange[i], &throwRange[i]);
    initState.x[i] = initPos;
    Move[i] = cnt++;
    Catch[i] = cnt++;
    Throw[i] = cnt++;
  }
  add(initState);
  while (!Q.empty()) {
    bool catched[3] = {false, false, false};
    P cur(Q.front());
    Q.pop();
    for (int i = 0; i < 3; ++i)
      if (cur.h[i] >= 0) catched[cur.h[i]] = true;
    int mask = cur.mask;
    for (int i = 0; i < 3; ++i) {
      if (catched[i]) continue;
      if (!(mask & 1 << Move[i]) && cur.h[i] == -1) {
        for (int d = 1; d <= moveRange[i]; ++d) {
          for (int dir = 0; dir < 2; ++dir) {
            int dd = cur.x[i] + d * (dir * 2 - 1);
            bool good = dd > 0;
            for (int k = 0; k < 3 && good; ++k) {
              if (i != k && !catched[k] && cur.x[k] == dd) {
                good = false;
              }
            }
            if (good) {
              P next(cur);
              next.x[i] = dd;
              next.mask |= 1 << Move[i];
              add(next);
            }
          }
        }
      }
      if (!(mask & 1 << Catch[i]) && cur.h[i] == -1) {
        for (int k = 0; k < 3; ++k) {
          if (k != i && !catched[k] && abs(cur.x[k] - cur.x[i]) == 1) {
            P next(cur);
            next.h[i] = k;
            next.mask |= 1 << Catch[i];
            add(next);
          }
        }
      }
      if (cur.h[i] >= 0 && !(mask & 1 << Throw[i])) {
        int j = cur.h[i];
        int k = cur.h[j];
        for (int d = 1; d <= throwRange[i]; ++d) {
          for (int dir = 0; dir < 2; ++dir) {
            int x2 = cur.x[i] + d * (dir * 2 - 1), x3;
            if (k >= 0) {
              x3 = x2 + cur.x[k] - cur.x[j];
              if (x2 != cur.x[i] && x2 > 0) {
                P next(cur);
                next.h[i] = -1;
                next.x[j] = x2;
                next.x[k] = x3;
                next.mask |= 1 << Throw[i];
                add(next);
              }
            } else {
              bool good = x2 > 0;
              for (int k = 0; k < 3 && good; ++k) {
                if (k != j && !catched[k] && cur.x[k] == x2) {
                  good = false;
                }
              }
              if (good) {
                P next(cur);
                next.h[i] = -1;
                next.x[j] = x2;
                next.mask |= 1 << Throw[i];
                add(next);
              }
            }
          }
        }
      }
    }
  }
  printf("%d\n", result);
  return 0;
}