1. Abridged Problem Statement  
You are given a directed network of N nodes (1..N) and M pipes (edges). Each pipe i goes from Ui to Vi, has capacity Zi, and a flag Ci which is 1 if this pipe must be fully filled (i.e., its flow equals Zi), or 0 otherwise (i.e., its flow can be anywhere between 0 and Zi). Substance is produced at node 1 with speed X ≥ 0, consumed at node N with the same speed X, and all other nodes obey flow conservation (no storage). Find the minimal X for which there exists a feasible flow meeting every pipe’s lower bound requirement, and output X and the flow on each pipe. If no such X exists, print “Impossible.”  

---

2. Detailed Editorial  

Problem restated as a circulation with lower bounds on certain edges, plus a single “circulation‐creator” edge from sink N back to source 1 of capacity X (variable). Lower‐bounded edges are those with Ci=1 and Zi capacity, so the lower bound equals the capacity Zi; upper‐bounded edges (Ci=0) have lower bound 0 and upper bound Zi. We must pick the smallest X such that a feasible circulation with those bounds exists.

Main steps:  
1. Transform the network to handle lower bounds.  
   - For each edge u→v with lower bound lb and upper bound ub, replace it with capacity (ub − lb) and record a demand: deg[v] += lb, deg[u] -= lb.  
2. Introduce a super‐source S and super‐sink T.  
   - For every node i with deg[i] > 0, add edge S→i of capacity deg[i].  
   - For every node i with deg[i] < 0, add edge i→T of capacity −deg[i].  
3. Add the “circulation creator” edge N→1 of capacity X, lower bound 0, treated like any regular edge with lb=0.  
4. On this transformed graph (with all lb=0 now), run a max‐flow from S to T.  
   - If the total flow equals the sum of all positive deg[i], then a feasible circulation exists.  
   - Otherwise, it’s impossible for that X.  
5. Binary‐search X over [0, high], where high can be, for instance, 1e8. For each mid, rebuild the transformed graph and test feasibility. Record the smallest feasible X.  
6. If no feasible X found, output “Impossible.” Otherwise, rebuild the graph with that X, run the flow, then recover each original edge’s flow as: actual flow = (flow on transformed edge) + lower bound.  

Complexities:  
- N ≤ 100, M ≤ N(N−1)/2 ≈ 5000, capacities ≤ 1e5, we do O(log C) ≈ 30 flow computations.  
- Using Dinic (or similar) is perfectly adequate under 0.5 s.

---

3. C++ Solution

```cpp
#include <bits/stdc++.h>
// #include <coding_library/graph/maxflow.hpp>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

template<class T>
class MaxFlow {
  private:
    const static T INF = numeric_limits<T>::max();

    struct Edge {
        T flow, cap;
        int idx, rev, to;
        Edge(int _to, int _rev, T _flow, T _cap, int _idx)
            : to(_to), rev(_rev), flow(_flow), cap(_cap), idx(_idx) {}
    };

    vector<int> dist, po;
    int n;

    bool bfs(int s, int t) {
        fill(dist.begin(), dist.end(), -1);
        fill(po.begin(), po.end(), 0);

        queue<int> q;
        q.push(s);
        dist[s] = 0;

        while(!q.empty()) {
            int u = q.front();
            q.pop();

            for(Edge e: adj[u]) {
                if(dist[e.to] == -1 && e.flow < e.cap) {
                    dist[e.to] = dist[u] + 1;
                    q.push(e.to);
                }
            }
        }
        return dist[t] != -1;
    }

    T dfs(int u, int t, T fl = INF) {
        if(u == t) {
            return fl;
        }

        for(; po[u] < (int)adj[u].size(); po[u]++) {
            auto& e = adj[u][po[u]];
            if(dist[e.to] == dist[u] + 1 && e.flow < e.cap) {
                T f = dfs(e.to, t, min(fl, e.cap - e.flow));
                e.flow += f;
                adj[e.to][e.rev].flow -= f;
                if(f > 0) {
                    return f;
                }
            }
        }

        return 0;
    }

  public:
    MaxFlow(int n = 0) { init(n); }

    vector<vector<Edge>> adj;

    void init(int _n) {
        n = _n;
        adj.assign(n + 1, {});
        dist.resize(n + 1);
        po.resize(n + 1);
    }

    void add_edge(int u, int v, T w, int idx = -1) {
        adj[u].push_back(Edge(v, adj[v].size(), 0, w, idx));
        adj[v].push_back(Edge(u, adj[u].size() - 1, 0, 0, -1));
    }

    T flow(int s, int t) {
        assert(s != t);

        T ret = 0, to_add;
        while(bfs(s, t)) {
            while((to_add = dfs(s, t))) {
                ret += to_add;
            }
        }

        return ret;
    }
};

int n, m;
vector<tuple<int, int, int, int>> edges;

void read() {
    cin >> n >> m;
    edges.resize(m);
    for(int i = 0; i < m; i++) {
        int u, v, cap, flag;
        cin >> u >> v >> cap >> flag;
        u--, v--;
        edges[i] = {u, v, cap, flag ? cap : 0};
    }
}

pair<vector<int>, MaxFlow<int>> build_circulation(int x) {
    MaxFlow<int> mf(n + 2);

    vector<int> deg(n);
    for(int i = 0; i < m; i++) {
        auto [u, v, cap, lb] = edges[i];
        deg[v] += lb;
        deg[u] -= lb;
        mf.add_edge(u, v, cap - lb, i);
    }

    for(int i = 0; i < n; i++) {
        if(deg[i] > 0) {
            mf.add_edge(n, i, deg[i]);
        } else if(deg[i] < 0) {
            mf.add_edge(i, n + 1, -deg[i]);
        }
    }

    mf.add_edge(n - 1, 0, x);
    return {deg, mf};
}

bool check_circulation(int x) {
    auto [deg, mf] = build_circulation(x);
    int need_flow = 0;
    for(int i = 0; i < n; i++) {
        if(deg[i] > 0) {
            need_flow += deg[i];
        }
    }

    return mf.flow(n, n + 1) == need_flow;
}

void solve() {
    // Minimum-source flow with lower bounds. Edges marked Ci = 1 must be fully
    // filled, so they have lower bound = upper bound = capacity; the rest have
    // lower bound 0. We binary search on the source production speed x: add a
    // back edge from node N-1 to node 0 with capacity x to turn the s-t flow
    // into a circulation, reduce the lower bounds in the standard way (each
    // edge u->v with lower bound lb contributes deg[v] += lb, deg[u] -= lb and
    // a residual edge of capacity cap - lb), and add a super source/sink that
    // supplies/drains the excess. A feasible circulation exists for a given x
    // iff the super-source-to-super-sink max flow saturates all positive
    // demands. We take the smallest feasible x, then read the actual per-pipe
    // flow as lower bound plus residual flow.

    int low = 0, high = (int)1e8, mid, ans = -1;
    while(low <= high) {
        mid = (low + high) / 2;
        if(check_circulation(mid)) {
            ans = mid;
            high = mid - 1;
        } else {
            low = mid + 1;
        }
    }

    if(ans == -1) {
        cout << "Impossible\n";
    } else {
        MaxFlow<int> mf = build_circulation(ans).second;
        mf.flow(n, n + 1);

        cout << ans << '\n';
        vector<int> flow(m);
        for(int u = 0; u < n; u++) {
            for(auto e: mf.adj[u]) {
                if(e.idx != -1) {
                    flow[e.idx] = e.flow + get<3>(edges[e.idx]);
                }
            }
        }

        cout << flow << '\n';
    }
}

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

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    return 0;
}
```

---

4. Python Solution with Detailed Comments  

```python
import sys
sys.setrecursionlimit(10**7)

class Dinic:
    """Dinic's max flow implementation."""
    def __init__(self, n):
        self.n = n
        self.adj = [[] for _ in range(n)]
    def add_edge(self, u, v, c, idx=-1):
        # forward edge: (to, cap, flow, rev_index, original_idx)
        self.adj[u].append([v, c, 0, len(self.adj[v]), idx])
        # reverse edge: cap=0
        self.adj[v].append([u, 0, 0, len(self.adj[u]) - 1, -1])
    def bfs(self, s, t, level):
        for i in range(self.n):
            level[i] = -1
        queue = [s]
        level[s] = 0
        for u in queue:
            for v, cap, flow, rev, _ in self.adj[u]:
                if level[v] < 0 and flow < cap:
                    level[v] = level[u] + 1
                    queue.append(v)
        return level[t] >= 0
    def dfs(self, u, t, f, level, ptr):
        if u == t:
            return f
        for i in range(ptr[u], len(self.adj[u])):
            v, cap, flow, rev, _ = self.adj[u][i]
            if level[v] == level[u] + 1 and flow < cap:
                pushed = self.dfs(v, t, min(f, cap-flow), level, ptr)
                if pushed:
                    # update flows
                    self.adj[u][i][2] += pushed
                    self.adj[v][rev][2] -= pushed
                    return pushed
            ptr[u] += 1
        return 0
    def max_flow(self, s, t):
        flow = 0
        level = [-1]*self.n
        while self.bfs(s, t, level):
            ptr = [0]*self.n
            while True:
                pushed = self.dfs(s, t, 10**18, level, ptr)
                if not pushed:
                    break
                flow += pushed
        return flow

def read_input():
    n, m = map(int, sys.stdin.readline().split())
    edges = []
    for _ in range(m):
        u, v, cap, flag = map(int, sys.stdin.readline().split())
        # convert to 0-based
        u -= 1
        v -= 1
        lb = cap if flag == 1 else 0
        edges.append((u, v, cap, lb))
    return n, m, edges

def build_graph(n, edges, x):
    """Build transformed graph for checking circulation with extra capacity x."""
    S, T = n, n+1
    mf = Dinic(n+2)
    demand = [0]*n
    # 1) transform edges with lower bounds
    for idx, (u, v, cap, lb) in enumerate(edges):
        demand[v] += lb
        demand[u] -= lb
        # add edge with capacity = cap - lb
        mf.add_edge(u, v, cap-lb, idx)
    # 2) fulfill demands via super-source/sink
    for i in range(n):
        if demand[i] > 0:
            # need additional inflow
            mf.add_edge(S, i, demand[i])
        elif demand[i] < 0:
            # has excess, must send out
            mf.add_edge(i, T, -demand[i])
    # 3) add circulation-creator from N-1 to 0
    mf.add_edge(n-1, 0, x)
    return mf, demand

def feasible(n, edges, x):
    mf, demand = build_graph(n, edges, x)
    # sum of positive demands
    need = sum(d for d in demand if d > 0)
    got = mf.max_flow(n, n+1)
    return got == need

def main():
    n, m, edges = read_input()

    # binary search for minimal x
    lo, hi = 0, 10**8
    ans = -1
    while lo <= hi:
        mid = (lo + hi) // 2
        if feasible(n, edges, mid):
            ans = mid
            hi = mid-1
        else:
            lo = mid+1

    if ans < 0:
        print("Impossible")
        return

    # rebuild final graph, run flow, then extract flows
    mf, demand = build_graph(n, edges, ans)
    mf.max_flow(n, n+1)
    res = [0]*m
    for u in range(n):
        for v, cap, flow, rev, idx in mf.adj[u]:
            if idx >= 0:
                # actual flow = recorded flow + lower bound
                lb = edges[idx][3]
                res[idx] = flow + lb

    print(ans)
    print(*res)

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

---

5. Compressed Editorial  

- Model pipes with capacity Zi and lower bound lb = Zi if Ci=1, else lb=0.  
- Add an extra edge from sink to source with capacity X.  
- Use standard lower‐bounds→circulation transform: replace each edge’s capacity by (Zi–lb), accumulate node demands, then link demanded/surplus nodes to a super‐source/sink.  
- For a candidate X, run max flow from super‐source to super‐sink; feasible iff it meets total demand.  
- Binary‐search X.  
- After finding minimal X, reconstruct flows by adding back the lower bounds.