Problem: Author note: I think some of you might remember the problem "Two Melodies" from Eductational Codeforces Round 22. Now it's time to make it a bit more difficult!

Alice is a composer, and recently she had recorded two tracks that became very popular. Now she has got a lot of fans who are waiting for new tracks. 

This time Alice wants to form four melodies for her tracks.

Alice has a sheet with n notes written on it. She wants to take four such non-empty non-intersecting subsequences that all of them form a melody and sum of their lengths is maximal.

Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.

Subsequence forms a melody when each two adjacent notes either differ by 1 or are congruent modulo 7.

You should write a program which will calculate maximum sum of lengths of such four non-empty non-intersecting subsequences that all of them form a melody.

Input

The first line contains one integer number n (4 ≤ n ≤ 3000).

The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 105) — notes written on a sheet.

Output

Print maximum sum of lengths of such four non-empty non-intersecting subsequences that all of them form a melody.

Examples

Input

5
1 3 5 7 9


Output

4


Input

5
1 3 5 7 2


Output

5

Note

In the first example it is possible to compose 4 melodies by choosing any 4 notes (and each melody will consist of only one note).

In the second example it is possible to compose one melody with 2 notes — {1, 2}. Remaining notes are used in other three melodies (one note per each melody).

Solution (in JAVA):

Solution:
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.Collection;
import java.util.Set;
import java.util.InputMismatchException;
import java.util.NavigableSet;
import java.io.IOException;
import java.util.TreeSet;
import java.util.ArrayList;
import java.util.List;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.Queue;
import java.util.ArrayDeque;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Rustam Musin (PloadyFree@gmail.com)
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        TaskG solver = new TaskG();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskG {
        List<TaskG.Edge>[] g;
        int[] phi;

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.readInt();
            int[] a = IOUtils.readIntArray(in, n);
            buildGraph(a);

            phi = findPhi();
//        if (true) return;

            for (int iter = 0; iter < 4; iter++) {
                Pair<int[], TaskG.Edge[]> dijkstra = dijkstra(2 * n);
                TaskG.Edge[] parentEdge = dijkstra.second;
                if (parentEdge[2 * n + 1] == null) {
                    break;
                }
                for (int v = 2 * n + 1; parentEdge[v] != null; v = parentEdge[v].from) {
                    parentEdge[v].flow++;
                    parentEdge[v].reversed.flow--;
                }
                int[] dist = dijkstra.first;
                for (int v = 0; v < g.length; v++) {
                    if (dist[v] != Integer.MAX_VALUE) {
                        phi[v] += dist[v];
                    }
                }
            }

            int ans = 0;
            for (int i = 0; i < n; i++) {
                ans += g[2 * i].get(0).flow;
            }

            out.print(ans);
        }

        Pair<int[], TaskG.Edge[]> dijkstra(int from) {
            NavigableSet<IntIntPair> q = new TreeSet<>();   //{dist, to}
            int[] dist = ArrayUtils.createArray(g.length, Integer.MAX_VALUE);
            TaskG.Edge[] cameWith = new TaskG.Edge[g.length];
            dist[from] = 0;
            q.add(IntIntPair.makePair(0, from));
            while (!q.isEmpty()) {
                int cur = q.pollFirst().second;
                for (TaskG.Edge e : g[cur]) {
                    if (e.capacity - e.flow > 0) {
                        int newDist = dist[e.from] + getCost(e);
                        if (newDist < dist[e.to]) {
                            q.remove(IntIntPair.makePair(dist[e.to], e.to));
                            q.add(IntIntPair.makePair(dist[e.to] = newDist, e.to));
                            cameWith[e.to] = e;
                        }
                    }
                }
            }
            return Pair.makePair(dist, cameWith);
        }

        int getCost(TaskG.Edge e) {
            return phi[e.from] + e.cost - phi[e.to];
        }

        int[] findPhi() {
            int[] deg = new int[g.length];
            for (int i = 0; i < g.length; i++) {
                for (TaskG.Edge e : g[i]) {
                    if (e.capacity == 1) {
                        deg[e.to]++;
                    }
                }
            }
            Queue<Integer> q = new ArrayDeque<>(g.length);
            int[] phi = ArrayUtils.createArray(g.length, Integer.MAX_VALUE);
            for (int i = 0; i < deg.length; i++) {
                if (deg[i] == 0) {
                    q.add(i);
                    phi[i] = 0;
                }
            }
            while (!q.isEmpty()) {
                int cur = q.poll();
                for (TaskG.Edge e : g[cur]) {
                    if (e.capacity == 1) {
                        phi[e.to] = Math.min(phi[e.to], phi[cur] + e.cost);
                        if (--deg[e.to] == 0) {
                            q.add(e.to);
                        }
                    }
                }
            }
            return phi;
        }

        void buildGraph(int[] a) {
            int n = a.length;
            g = new List[2 * n + 2];
            for (int i = 0; i < g.length; i++) {
                g[i] = new ArrayList<>(n + 1);
            }
            for (int i = 0; i < n; i++) {
                addEdge(2 * i, 2 * i + 1, -1);
            }
            for (int i = 0; i < n; i++) {
                for (int j = i + 1; j < n; j++) {
                    if (way(a[i], a[j])) {
                        addEdge(2 * i + 1, 2 * j, 0);
                    }
                }
            }
            for (int i = 0; i < n; i++) {
                addEdge(2 * n, 2 * i, 0);
                addEdge(2 * i + 1, 2 * n + 1, 0);
            }
        }

        static boolean way(int a, int b) {
            return Math.abs(a - b) == 1 || ((a % 7) == (b % 7));
        }

        void addEdge(int from, int to, int cost) {
            TaskG.Edge e1 = new TaskG.Edge(from, to, cost, 1);
            TaskG.Edge e2 = new TaskG.Edge(to, from, -cost, 0);
            e1.reversed = e2;
            e2.reversed = e1;
            g[from].add(e1);
            g[to].add(e2);
        }

        static class Edge {
            int from;
            int to;
            int cost;
            int capacity;
            int flow;
            TaskG.Edge reversed;

            public Edge(int from, int to, int cost, int capacity) {
                this.from = from;
                this.to = to;
                this.cost = cost;
                this.capacity = capacity;
            }

        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int readInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }

    static class IOUtils {
        public static int[] readIntArray(InputReader in, int size) {
            int[] array = new int[size];
            for (int i = 0; i < size; i++) {
                array[i] = in.readInt();
            }
            return array;
        }

    }

    static class ArrayUtils {
        public static int[] createArray(int count, int value) {
            int[] array = new int[count];
            Arrays.fill(array, value);
            return array;
        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void close() {
            writer.close();
        }

        public void print(int i) {
            writer.print(i);
        }

    }

    static class Pair<U, V> implements Comparable<Pair<U, V>> {
        public final U first;
        public final V second;

        public static <U, V> Pair<U, V> makePair(U first, V second) {
            return new Pair<U, V>(first, second);
        }

        private Pair(U first, V second) {
            this.first = first;
            this.second = second;
        }


        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }

            Pair pair = (Pair) o;

            return !(first != null ? !first.equals(pair.first) : pair.first != null) &&
                    !(second != null ? !second.equals(pair.second) : pair.second != null);
        }


        public int hashCode() {
            int result = first != null ? first.hashCode() : 0;
            result = 31 * result + (second != null ? second.hashCode() : 0);
            return result;
        }


        public String toString() {
            return "(" + first + "," + second + ")";
        }

        @SuppressWarnings({"unchecked"})
        public int compareTo(Pair<U, V> o) {
            int value = ((Comparable<U>) first).compareTo(o.first);
            if (value != 0) {
                return value;
            }
            return ((Comparable<V>) second).compareTo(o.second);
        }

    }

    static class IntIntPair implements Comparable<IntIntPair> {
        public final int first;
        public final int second;

        public static IntIntPair makePair(int first, int second) {
            return new IntIntPair(first, second);
        }

        public IntIntPair(int first, int second) {
            this.first = first;
            this.second = second;
        }


        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }

            IntIntPair pair = (IntIntPair) o;

            return first == pair.first && second == pair.second;
        }


        public int hashCode() {
            int result = first;
            result = 31 * result + second;
            return result;
        }


        public String toString() {
            return "(" + first + "," + second + ")";
        }

        @SuppressWarnings({"unchecked"})
        public int compareTo(IntIntPair o) {
            int value = Integer.compare(first, o.first);
            if (value != 0) {
                return value;
            }
            return Integer.compare(second, o.second);
        }

    }
}