<|instruction|>
Solve the below problem. The solution should start with an abridged problem statement. Then key observations. Then full solution based on the observations. Then C++ and Python implementations with comments.

533. Dice Tower
Time limit per test: 1 second(s)
Memory limit: 262144 kilobytes
input: standard
output: standard

Polycarp loves not only to play games, but to invent ones as well. He has recently been presented with a board game which also had lots of dice. Polycarp quickly noticed an interesting phenomenon: the sum of dots on any two opposite sides equals 7.


The dice

An unfolded die
Polycarp invented the following game. He asks somebody to tell a positive integer n and then he constructs a dice tower putting the dice one on another one. A tower is constructed like that: Polycarp puts a die on the table and then (if he wants) he adds more dice, each time stacking a new die on the top of the tower. The dice in the tower are aligned by their edges so that they form a perfect rectangular parallelepiped. The parallelepiped's height equals the number of dice in the tower and two other dimensions equal 1 (if we accept that a die's side is equal to 1).


An example of a tower whose height equals 3
Polycarp's aim is to build a tower of minimum height given that the sum of points on all its outer surface should equal the given number n (outer surface: the side surface, the top and bottom faces).

Write a program that would determine the minimum number of dice in the required tower by the given number n. Polycarp can construct any towers whose height equals 1 or more.

Input
The only input line contains integer n (1 ≤ n ≤ 106).

Output
Print the only integer — the number of dice in the required tower. If no such tower exists, print -1.

Example(s)
sample input
sample output
50
3

sample input
sample output
7
-1

sample input
sample output
32
2

<|response|>
1. Abridged Problem Statement  
Given a positive integer n (1 ≤ n ≤ 10^6), you must build a 1×1×k tower of standard dice (stacked face to face). Each die has opposite faces summing to 7. The sum of all exposed pips (the four side faces of each die, plus the very bottom face of the bottom die and the very top face of the top die) must equal n. Find the minimum k≥1 for which this is possible, or output –1 if no solution exists.

2. Key Observations  
- A single die has six faces summing to 1+2+3+4+5+6 = 21.  
- In a stacked tower, the interior faces between dice are hidden, so each die contributes exactly its four side faces (the lateral faces) plus—only for the topmost die its top face, and only for the bottommost die its bottom face.  
- The sum of any two opposite faces is always 7.  
- For each die in the middle of the tower, exactly four side faces are exposed; its top and bottom faces (which sum to 7) are hidden. So each such die contributes 21 – 7 = 14 pips from its four side faces.  
- The bottommost die contributes 14 pips from its four sides plus the value x ∈ [1…6] on its bottom face.  
- The topmost die contributes 14 pips from its four sides plus the value y ∈ [1…6] on its top face.  
- Total exposed pips S = 14·k + x + y, where k is number of dice, x and y are in [1…6].  
- Special case k=1: all six faces are exposed, so S≡21. In the formula S = 14·1 + x + y we would have x+y=7 exactly (because top and bottom are opposite faces), yielding S=21.

3. Full Solution Approach  
1. If n=21, the answer is k=1 (one die shows all faces).  
2. Otherwise assume k≥2.  Let k = ⌊n / 14⌋ (the maximum number of “full 14’s” not exceeding n) and rem = n − 14·k.  
3. We need rem = x + y for some x,y ∈ [1…6]. Hence 2 ≤ rem ≤ 12.  
4. If rem falls in [2…12], we can pick x and y to sum to rem, and k is our answer.  
5. Otherwise no valid tower exists, print –1.

Edge conditions:  
- If k computed as above is 0 (i.e. n<14) and n≠21, it is impossible.  
- If k=1 but n≠21, no solution (because a single die always sums to 21).  
- If rem<2 or rem>12, no pair of faces can give that sum.

Time complexity is O(1) and memory O(1).

4. C++ Implementation with Detailed Comments

```cpp
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;

    // Special one-die case
    if (n == 21) {
        // Exactly one die shows all six faces summing to 21
        cout << 1 << "\n";
        return 0;
    }

    // For k>=2, factor out as many full '14 per die' chunks as possible
    int k = n / 14;
    int rem = n - 14 * k;  // remainder to be covered by top + bottom

    // If we have fewer than 2 dice, or remainder is not in [2..12], no solution
    if (k < 2 || rem < 2 || rem > 12) {
        cout << -1 << "\n";
    } else {
        // We can always choose two face values x,y in [1..6] with x+y=rem
        cout << k << "\n";
    }

    return 0;
}
```

5. Python Implementation with Detailed Comments

```python
def solve():
    n = int(input().strip())

    # Case: exactly one die (all 6 faces exposed) => sum must be 21
    if n == 21:
        print(1)
        return

    # Try k >= 2 dice, each contributing 14 from side faces
    k = n // 14
    rem = n - 14 * k  # must be covered by top + bottom pips

    # We need k >= 2 and 2 <= rem <= 12 to pick two faces in [1..6]
    if k < 2 or rem < 2 or rem > 12:
        print(-1)
    else:
        print(k)

if __name__ == "__main__":
    solve()
```

Explanation of key steps:  
- We immediately handle n=21 as the only valid one-die configuration.  
- For larger n, we greedily use as many 14-pip contributions as possible (each middle die) and then see if the leftover rem can be split into two faces in [1…6].  
- If rem is between 2 and 12 inclusive, we succeed with k dice; otherwise, no solution exists.